Forum: Ruby on Rails Including an extra variable

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Martin S. (Guest)
on 2006-04-24 14:17
(Received via mailing list)
Hi,

I´m adding numbers to a database from fields in a form, but I want to
include some information I have in a variable not generated in this
form.
The variable user.company exists in the same class as create(), and I
want to include the value of this variable when I add new numbers to
the database.

The form looks like this:
<p><label for="number_number">Number</label><br/>
<%= text_field 'number', 'number'  %></p>

<p><label for="number_name">Name</label><br/>
<%= text_field 'number', 'name'  %></p>


   def create
     @number = Number.new(params[:number])
   end

How can I make "create" include the value of user.company or $company?
This should be put in the "company" field of the Numbers database.

Numbers contains the following fields: id, number, name, email, list,
company


Best regards,
Martin S._______________________________________________
Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails
Steve K. (Guest)
on 2006-04-24 19:18
This isn't a relational way to do it, but it is what you asked for...

   def create
      @user = your_code_for_however_you_get_your_user_if_necessary
      @number = Number.new(params[:number])
      @number.company = @user.company
      @number.save
   end

Martin S. wrote:
> Hi,
>
> I�m adding numbers to a database from fields in a form, but I want to
> include some information I have in a variable not generated in this
> form.
> The variable user.company exists in the same class as create(), and I
> want to include the value of this variable when I add new numbers to
> the database.
>
> The form looks like this:
> <p><label for="number_number">Number</label><br/>
> <%= text_field 'number', 'number'  %></p>
>
> <p><label for="number_name">Name</label><br/>
> <%= text_field 'number', 'name'  %></p>
>
>
>    def create
>      @number = Number.new(params[:number])
>    end
>
> How can I make "create" include the value of user.company or $company?
> This should be put in the "company" field of the Numbers database.
>
> Numbers contains the following fields: id, number, name, email, list,
> company
>
>
> Best regards,
> Martin S._______________________________________________
> Rails mailing list
> removed_email_address@domain.invalid
> http://lists.rubyonrails.org/mailman/listinfo/rails
This topic is locked and can not be replied to.