ruby on rails - Form for one model in view for another -
i writing simple rails app , have 2 models, location user, have same relationship message model. is, location has many messages, user has many messages, , message belongs user , location. trying add form create new message on location show view. here code:
app/views/locations/show.html.erb: <h1><%= @locaton.name %></h1> <p><%= @location.location %></p> <p><%= @location.discription %></p> <%= link_to "find different location", crags_path %> <h2> messages <ul> <% @location.messages.each |message|%> </li> <h3><%=message.user.username%></h3> <p><%=message.content%></p> </li> <% end %> </ul>
locations controller:
class locationscontroller < applicationcontroller def index @locations = location.all end def show @location = location.find(params[:id]) end def new @location = location.new end def create @location = location.new( name: params[:location][:name], location: params[:location][:location], discription: params[:location][:discription]) @location.save flash.notice = "#{@location.name} created!" redirect_to location_path(@crag) end def edit @location = location.find(params[:id]) end def update @location = location.find(params[:id]) @location.update_attributes(params[:location]) flash.notice = "#{@location.name} updated!" redirect_to crag_path(@location) end def destroy @location = location.find(params[:id]) @location.destroy flash.notice = "#{@location.name} deleted." redirect_to locations_path end end
messagescontroller
class messagescontroller < applicationcontroller def new @message = message.new end def create @message = current_user.messages.build( content: params[:message][:content]) redirect_to crags_path end end
form partial trying include in location/show.html.erb
<%= form_for(@message) |f| %> <p> <%= f.label "leave message" %> <%= f.text_area :content %> </p> <%= f.submit "submit" %> <% end %>
when try include partial following error
undefined method `model_name' nilclass:class
and assume form trying communicate locations controller since in locations view, , since cant find instance @message in locations controller, doesn't know do. trying figure out how include form messages controller in view locations. if create view new_message_path works fine, again, guessing because functioning within message views referes messages controller.
i wondering if there way creat message linked both current_user , location form (or when figure out last problem. message object has location_id attribute, how use page on (with url /locations/location_id) link message location? thanks!
firstly, you'll need set @message
variable new message in controller:
# locations_controller.rb def show @location = location.find(params[:id]) @message = @location.messages.build(user_id: current_user.id) end
using @location.messages.build
automatically populate location_id
@location.id
new message, , we're setting user_id
here too.
with in place, should able build form in page (although might need add hidden fields store user_id
, location_id
@message
).
with done, can trim down messages_controller
too. unless want have additional page create new message not tied location, can rid of new
action (and route). can simplify create
action to:
# messages_controller.rb def create @message = message.new(params[:message]) if @message.save # whatever want on successful save, example: flash[:success] = "message created." redirect_to location_path(@message.location_id) else # whatever want if saving fails end end
Comments
Post a Comment