我使用简单的形式作为嵌套资源。
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
答案 0 :(得分:1)
解决:
轻松修复 - 需要在新操作中设置@user = current_user:
def new
@interest = current_user.interests.new
@user =current_user
end