2017-02-01 75 views
0

這樣的IM發送從視圖參數去另一個控制器訪問PARAM通過渲染sended另一個控制器:模板

<%= render template: 'dishes/new', :id => @restaurant.id %> 

的觀點,這是我的其他控制器的看法:

def new 
    @dish = Dish.new 
    @id = params[:id] 
    end 

    def create 
    @dish = Dish.new(dish_params) 

    respond_to do |format| 
     if @dish.save 
     format.html { redirect_to @restaurant, notice: 'dish was successfully created.' } 
     format.json { render action: 'show', status: :created, location: :restaurant } 
     else 
     format.html { render action: 'show', location: :restaurant } 
     format.json { render json: @restaurant.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

private 
    def dish_params 
    params.require(:dish).permit(:avatar, :name, :description, [:id]) 
    end 

,這是我的看法誰調用形式:

#new.html.erb 
<h1>Add a new dish</h1> 

<%= render 'dishes/dish_form' %> 

那麼這裏就是形式:

<%= form_for @dish, :url => { :controller => "dishes", action_name => "create" } do |f| %> 
    <div class="field"> 
    <%= f.label :dish_name %> 
    <%= f.text_field :name %> 
    </div> 
    <div class="field"> 
    <%= f.label :description %> 
    <%= f.text_field :description %> 
    </div> 
    <div class="field"> 
    <%= f.label :image %> 
    <%= f.file_field :avatar %> 
    </div> 
    <div class="actions"> 
    <%= f.submit 'Add new dish' %> 
    </div> 
<% end %> 

,但是當我試圖挽救它,我得到這個錯誤: error

+1

在你的控制器中沒有行動'show' –

+0

是的我知道我會在稍後解決這個問題,問題進入那裏是因爲沒有餐廳ID – imjustaguy

+0

您在'create'動作中調用'@ restaurant',但未定義'@ restaurant'。 –

回答

0

我認爲你的問題出現在你期待你的render template: 'dishes/new'打你的DishesController。當您調用渲染時,這並不是什麼情況。

你正在做的是告訴rails加載到你當前的視圖模板'dishes/new',並且你正在傳遞'dishes/new'一個變量,它可以在渲染時訪問。您沒有向控制器發送參數。

如果你想保持當前的設置,你可以使它調整一些。

我打算假設您的Dish模型屬於Restaurant,因此具有restaurant_id作爲列。如果這是真的,那麼你可以在你的表單中添加一個隱藏字段,名爲restaurant_id,並在控制器中更新允許的參數,以允許在餐具上設置餐廳ID。

您的形式是這樣的:

<%= form_for @dish, :url => { :controller => "dishes", action_name => "create" } do |f| %> 
    <%= f.hidden_field :restaurant_id, value: id %> 
    ... 
<% end %> 

你在你的控制器允許PARAMS應該是這樣的:

def dish_params 
    params.require(:dish).permit(:avatar, :name, :description, :restaurant_id) 
end 

你可以得到關於如何在軌渲染工作here

更多信息
+0

這就是我做和工作!但我使用此行 '<%= f.hidden_​​field:restaurant_id,value:params [id]%>' 因爲我已經獲得了參數,我嘗試在創建中放入'@id = params [:id]' Dishes的控制器並使用'<%= f.hidden_​​field:restaurant_id,value:@id%>'但沒有用,它只能在第一個 – imjustaguy

0

錯誤是說,你的控制器命名的餐館沒有名爲「秀」的動作。

一個解決方案是將名爲「Show」的Action添加到餐館控制器。

另一種選擇可能是您實際創建了您想要的動作,但是您將其命名爲「Show」以外的其他選項。

+0

是的,但它進入其他,因爲它沒有檢測到即時通訊發送的餐廳ID呈現 – imjustaguy

相關問題