DamnBigMan-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Jul-18 02:39 UTC
New entries from foreign controller
I have a situation that I can''t seem to find an answer for. I have a
view for a new "report" I want it to be possible for the user to input
a company name and location if they aren''t already in the list of
"known" values and when the form is submitted I want those manual
entries added to their respective tables at the same time the report
is added.
How would I go about doing this? I''ve tried creating new_company and
new_location fields in the view and looking for them in the controller
but they are never set in the params hash.
Below is the relevant code:
CONTROLLER:
def create
@report = Report.new(params[:report])
# If they entered a location that wasn''t listed we need to add it
if params[:new_location]
@location = Location.new
@location.city = params[:new_location][0]
@location.region = params[:new_location][1]
@location.country = params[:new_location][2]
@location.save
end
# Similar to above but if they entered a company that wasn''t lited
if params[:new_company]
@company = Company.new
@company.name = params[:new_company]
if params[:new_location]
@company.location_id = @location.id
end
@company.save
end
# And again for a new position
if params[:new_position]
@position = Position.new
@position.name = params[:new_position]
if params[:new_company]
@position.company_id = @company
end
@position.save
end
if @report.save
flash[:notice] = params.to_s
redirect_to :action => ''list''
else
render :action => ''new''
end
end
VIEW:
<p>
<label for="report_title">Title</label><br />
<%= text_field ''report'', ''title'' %>
</p>
<p>
<label for="report_company_id">Company:</label>
<select id="company_id", name="company_id">
<%= options_from_collection_for_select(@companies, :id, :name,
@selected) %>
</select>
</p>
<p>
<label for="report_new_company">Other:</label>
<%= text_field ''report'', ''company_id''
%>
</p>
<p>
<label for="report_position_id">Position:</label>
<select id="position_id", name="position_id">
<%= options_from_collection_for_select(@positions, :id, :name,
@selected) %>
</select>
</p>
<p>
<label for="report_new_position">Other:</label>
<%= text_field ''report'', ''position_id''
%>
</p>
<p>
<label for="report_category_id">Category:</label>
<select id="category_id", name="category_id">
<%= options_from_collection_for_select(@categories, :id, :name,
@selected) %>
</select>
</p>
<p>
<label for="report_new_category">Other:</label>
<%= text_field ''report'', ''category_id''
%>
</p>
Thanks in advance,
Glen
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
DamnBigMan-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Jul-18 03:12 UTC
Re: New entries from foreign controller
Nevermind, I should have poked it with the "stick" a few more times before I posted. Using <%= text_field_tag ''new_thing'' %> rather than <%= text_field ''report'', ''thing'' %> gave me the values in params I was looking for. On Jul 17, 8:39 pm, "DamnBig...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org" <DamnBig...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I have a situation that I can''t seem to find an answer for. I have a > view for a new "report" I want it to be possible for the user to input > a company name and location if they aren''t already in the list of > "known" values and when the form is submitted I want those manual > entries added to their respective tables at the same time the report > is added. > > How would I go about doing this? I''ve tried creating new_company and > new_location fields in the view and looking for them in the controller > but they are never set in the params hash. > > Below is the relevant code: > > CONTROLLER: > > def create > @report = Report.new(params[:report]) > > # If they entered a location that wasn''t listed we need to add it > if params[:new_location] > @location = Location.new > @location.city = params[:new_location][0] > @location.region = params[:new_location][1] > @location.country = params[:new_location][2] > @location.save > end > > # Similar to above but if they entered a company that wasn''t lited > if params[:new_company] > @company = Company.new > @company.name = params[:new_company] > if params[:new_location] > @company.location_id = @location.id > end > @company.save > end > > # And again for a new position > if params[:new_position] > @position = Position.new > @position.name = params[:new_position] > if params[:new_company] > @position.company_id = @company > end > @position.save > end > > if @report.save > flash[:notice] = params.to_s > redirect_to :action => ''list'' > else > render :action => ''new'' > end > end > > VIEW: > > <p> > <label for="report_title">Title</label><br /> > <%= text_field ''report'', ''title'' %> > </p> > > <p> > <label for="report_company_id">Company:</label> > <select id="company_id", name="company_id"> > <%= options_from_collection_for_select(@companies, :id, :name, > @selected) %> > </select> > </p> > > <p> > <label for="report_new_company">Other:</label> > <%= text_field ''report'', ''company_id'' %> > </p> > > <p> > <label for="report_position_id">Position:</label> > <select id="position_id", name="position_id"> > <%= options_from_collection_for_select(@positions, :id, :name, > @selected) %> > </select> > </p> > > <p> > <label for="report_new_position">Other:</label> > <%= text_field ''report'', ''position_id'' %> > </p> > > <p> > <label for="report_category_id">Category:</label> > <select id="category_id", name="category_id"> > <%= options_from_collection_for_select(@categories, :id, :name, > @selected) %> > </select> > </p> > > <p> > <label for="report_new_category">Other:</label> > <%= text_field ''report'', ''category_id'' %> > </p> > > Thanks in advance, > Glen--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---