2013-04-18 93 views
0

我正在编写一个简单的rails应用程序,我目前有两个模型,位置用户,它们与消息模型具有相同的关系。即,位置有很多消息,用户有很多消息,消息属于用户和位置。我目前正在尝试添加一个表单,以在我的位置显示视图中创建一条新消息。下面是一些代码:一个模型在另一个视图中的形式

app/views/locations/show.html.erb: 

<h1><%= @locaton.name %></h1> 
<p><%= @location.location %></p> 
<p><%= @location.discription %></p> 
<%= link_to "Find a Different Location", crags_path %> 

<h2> Messages 
<ul> 
    <% @location.messages.each do |message|%> 
    </li> 
     <h3><%=message.user.username%></h3> 
     <p><%=message.content%></p> 
    </li> 
    <% end %> 
</ul> 

位置控制器:

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 

表部分,我想在的位置,包括

/show.html.erb

<%= form_for(@message) do |f| %> 
<p> 
    <%= f.label "Leave a Message" %> 
    <%= f.text_area :content %> 
</p> 
<%= f.submit "submit" %> 
<% end %> 

当我尝试添加UDE部分我得到以下错误

undefined method `model_name' for NilClass:Class 

,我认为形式试图与位置控制器进行通信,因为它是在一个位置查看,并因为它无法找到的位置控制器实例@message ,它不知道该怎么做。我想弄清楚如何在消息控制器中的位置视图中包含此窗体。如果我为new_message_path创建一个视图,它可以正常工作,我猜测它是因为它在消息视图中运行,所以它会反馈到消息控制器。

我还想知道是否有任何方法可以创建一个与current_user和表单所在位置相关的消息(或者希望在我找出最后一个问题的时候会产生一个消息对象有一个location_id属性,我该如何使用我当前所在的页面(使用url/locations/location_id)将消息链接到该位置?谢谢!

回答

0

首先,您需要将@message变量实际设置为新在控制器中的消息:

# locations_controller.rb 
def show 
    @location = Location.find(params[:id]) 
    @message = @location.messages.build(user_id: current_user.id) 
end 

使用@location.messages.build会自动填充th e location_id@location.id为新消息,我们也在这里设置user_id。在地方

有了这一点,你应该能够建立在你的页面的形式(虽然你可能需要添加一些隐藏的字段来存储@messageuser_idlocation_id)。

完成后,您也可以修剪您的messages_controller。除非你想有一个额外的页面来创建一个不绑定到某个位置的新消息,否则你可能会摆脱你的new动作(和路由)。然后你可以简化你的create行动:

# messages_controller.rb 
def create 
    @message = Message.new(params[:message]) 
    if @message.save 
    # whatever you want to do on successful save, for example: 
    flash[:success] = "Message created." 
    redirect_to location_path(@message.location_id) 
    else 
    # whatever you want to do if saving fails 
    end 
end 
+0

感谢您的帮助!正是我在找什么 – user2136807 2013-04-18 05:53:43

+0

这些都没有经过测试,所以你可能会碰到一些快速冲动,但这应该让你朝着正确的方向:) – 2013-04-18 05:55:50

+0

有一件事仍然不起作用的是, location_id和user_id没有出现在params散列中,它们都看起来像'<%f.hidden_​​field:user_id,:value => current_user.id%> – user2136807 2013-04-18 06:02:39

相关问题