這樣的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 %>
在你的控制器中沒有行動'show' –
是的我知道我會在稍後解決這個問題,問題進入那裏是因爲沒有餐廳ID – imjustaguy
您在'create'動作中調用'@ restaurant',但未定義'@ restaurant'。 –