2013-04-18 92 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

相關問題