【发布时间】:2015-02-22 11:54:13
【问题描述】:
我对嵌套资源使用简单的形式。
resources :users do
resources :interests
end
我的 InterestsController 中有以下回调
class InterestsController < ApplicationController
before_action :set_interest, only: [:show, :create, :edit, :update, :destroy]
before_action :set_user, only: [:index, :show, :create, :edit, :update, :destroy]
private
# Use callbacks to share common setup or constraints between actions.
def set_interest
@interest = Interest.find(params[:id])
end
def set_user
@user = current_user
end
end
我有一个简单的表格 - 这很有效......
<%= simple_form_for [:user, @interest] do |f| %>
<% if @interest.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@interest.errors.count, "error") %> prohibited this response from being saved:</h2>
<ul>
<% @interest.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-inputs">
<%= f.input :user_id %>
<%= f.input :discipline_id %>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
但这并没有.....
<%= simple_form_for [@user, @interest] do |f| %>
我找不到任何解释 - 简单形式的 @user 肯定是正确的,是声明用户?
这里有更多信息:
这是我在编辑操作时遇到的错误:
No route matches {:action=>"show", :controller=>"interests", :format=>nil, :id=>nil, :user_id=>#<Interest id: 1, user_id: 1, discipline_id: 10, created_at: "2015-02-22 11:00:14", updated_at: "2015-02-22 11:00:14">} missing required keys: [:id]
有趣的是,新操作有效。如果我将简单形式切换为
<%= simple_form_for [@user, @interest] do |f| %>
编辑操作有效,但新操作无效。所以我猜这一定是我设置我的 InterestsController 的方式。
这里是完整的。
class InterestsController < ApplicationController
before_action :set_interest, only: [:show, :create, :edit, :update, :destroy]
before_action :set_user, only: [:index, :show, :create, :edit, :update, :destroy]
def index
@users_interests = @user.interests
end
def show
end
def new
@interest = current_user.interests.new
end
def edit
end
def create
@interest = User.find(params[:user_id]).interests.build(interest_params)
@interest.user = current_user
respond_to do |format|
if @interest.save
flash[:success] = "Interest created!"
format.html { redirect_to user_interests_path(current_user) }
format.json { render :show, status: :created, location: @interest }
else
flash[:failure] = "Interest was not added!"
format.html { render :new }
format.json { render json: @interest.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if @interest.update(interest_params)
format.html { redirect_to user_interests_path(current_user) }
format.json { render :show, status: :ok, location: @interest }
else
format.html { render :edit }
format.json { render json: @interest.errors, status: :unprocessable_entity }
end
end
end
def destroy
@interest.destroy
respond_to do |format|
format.html { redirect_to interests_url, notice: 'Interest was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_interest
@interest = Interest.find(params[:id])
end
def set_user
@user = current_user
end
# Never trust parameters from the scary internet, only allow the white list through.
def interest_params
params.require(:interest).permit(:user_id, :discipline_id)
end
end
【问题讨论】:
-
请解释它是如何不起作用的,如果您遇到错误,请将其添加到问题中。
-
给你。我怀疑这是我的 InterestsController 的问题。我是 Rails 新手,如果这非常简单,我深表歉意
标签: ruby-on-rails forms nested