无法找到'id'=

时间:2017-05-22 13:24:03

标签: ruby-on-rails ruby ruby-on-rails-5.1

我有一个World父对象和一个State子对象。我正在尝试创建一个新的State对象,rails并没有找到世界ID。我正在尝试从世界展示页面链接到新状态表单,并且网址显示.../worlds/1/states/new,那么为什么这不会占用父ID?该错误应该来自控制器@world = World.find(params[:id])中的这一行。我尝试使用(params[:world_id])偶数。

为简洁起见,我只在这里发布相关代码。

world.rb

class World < ApplicationRecord
  belongs_to :user
  has_many :states
end

state.rb

class State < ApplicationRecord
  belongs_to :world
  belongs_to :user
end

states_controller.rb

class StatesController < ApplicationController
  before_action :set_state, only: [:show, :edit, :update, :destroy]
  before_action :authenticate_user!, except: [:index, :show]

  def index
    @states = State.all
  end

  def new
    @world = World.find(params[:id])
    @state = @world.states.build
  end

  def create
    @world = World.find(params[:id])
    @state = @world.states.build(state_params)
    @state.user = current_user

    respond_to do |format|
      if @state.save
        format.html { redirect_to @state, notice: 'State was successfully created.' }
      else
        format.html { render :new }
      end
    end
  end

  private
    def set_state
      @state = State.find(params[:id])
    end

    def state_params
      params.require(:state).permit(:name, :summary, :history, :population, :inception, :life_expectancy, :land_mass,
                                    :climate, :industry, :education, :mythology, :law, :culture, :world_id, :user_id)
    end
end 

worlds / show.html.erb中新状态表单的链接:

<%= link_to 'New State', new_world_state_path(@world) %>

的routes.rb

Rails.application.routes.draw do
  resources :states
  resources :worlds
  devise_for :users, path: '', path_names: { sign_in: 'login', sign_out: 'logout', sign_up: 'register' }
  root to: "home#index"

  resources :users
  resources :worlds do
    resources :states
  end
end

的状态/ _form.html.erb

<div class="form">
  <%= form_for(state) do |f| %>
    <% if state.errors.any? %>
      <div id="error_explanation">
        <h2><%= pluralize(world.errors.count, "error") %> prohibited this state from being saved:</h2>

        <ul>
        <% state.errors.full_messages.each do |message| %>
          <li><%= message %></li>
        <% end %>
        </ul>
      </div>
    <% end %>

    <%= f.text_field :name, placeholder: 'Name' %><br />

    <fieldset>
      <legend>Basic Info</legend>
        <%= f.text_area :summary, placeholder: 'Summary About', rows: 6 %><br />
        <%= f.text_area :history, placeholder: 'History', rows: 6 %><br />
        <%= f.text_area :climate, placeholder: 'Climate', rows: 3 %><br />
        <%= f.text_area :industry, placeholder: 'Industry', rows: 3 %><br />
        <%= f.text_area :education, placeholder: 'Education', rows: 3 %><br />
        <%= f.text_area :culture, placeholder: 'Culture', rows: 3 %><br />
        <%= f.text_area :law, placeholder: 'Legal System, Crime & Punishment', rows: 3 %><br />
        <%= f.text_area :mythology, placeholder: 'Mythology', rows: 3 %><br />
    </fieldset>

    <fieldset>
      <legend>Quick Stats</legend>
        <%= f.text_field :inception, placeholder: 'Inception' %><br />
        <%= f.text_field :population, placeholder: 'Population' %><br />
        <%= f.text_field :life_expectancy, placeholder: 'Ave. Life Expectance' %><br />
        <%= f.text_field :land_mass, placeholder: 'Land Mass' %><br />
    </fieldset>

    <p><%= f.submit %></p>
  <% end %>
</div>
单击“新建状态”链接

时,

导出控制台结果

Started GET "/worlds/1/states/new" for 70.196.17.76 at 2017-05-22 13:43:47 +0000
Cannot render console from 70.196.17.76! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by StatesController#new as HTML
  Parameters: {"world_id"=>"1"}
  User Load (0.3ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ?  [["id", 2], ["LIMIT", 1]]
  World Load (0.1ms)  SELECT  "worlds".* FROM "worlds" WHERE "worlds"."id" = ? LIMIT ?  [["id", nil], ["LIMIT", 1]]
Completed 404 Not Found in 3ms (ActiveRecord: 0.4ms)



ActiveRecord::RecordNotFound (Couldn't find World with 'id'=):

app/controllers/states_controller.rb:13:in `new'
  Rendering /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.3/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout
  Rendering /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.3/lib/action_dispatch/middleware/templates/rescues/_source.html.erb
  Rendered /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.3/lib/action_dispatch/middleware/templates/rescues/_source.html.erb (4.7ms)
  Rendering /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.3/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb
  Rendered /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.3/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (2.6ms)
  Rendering /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.3/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb
  Rendered /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.3/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.1ms)
  Rendered /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.3/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (36.6ms)

2 个答案:

答案 0 :(得分:2)

修改您的link_to帮助程序,以指定并告诉Rails您通过它发送的参数:

自:

<%= link_to 'New State', new_world_state_path(@world) %>

要:

<%= link_to 'New State', new_world_state_path(id: @world) %>

id,因为您试图通过World作为参数找到:id

还尝试更改在您设置param变量的控制器中收到的@world

def new
  @world = World.find(params[:world_id])
  ...
end

show.html.erb

<%= link_to 'New World', new_world_state_path(world_id: @world) %>

更新:我们做了什么:

app/views/worlds/show.html.erb中更改参数的设置方式:

自:

<%= link_to 'New Nation', new_world_state_path(world_id: @world_id) %> # @world_id wasn't defined

要:

<%= link_to 'New Nation', new_world_state_path(world_id: @world.id) %>

/app/views/states/_form.html.erb中将world_id添加为hidden_field

<%= f.hidden_field :world_id, value: @world.id %>

然后在app/controllers/states_controller.rb改变接受参数的方式:

def new
  @world = World.find(params[:world_id])
  @state = @world.states.build
end

def create
  @world = World.find(params[:state][:world_id])
  ...

答案 1 :(得分:2)

当world_id传递给:new操作时,它可能不会在表单上传回给创建操作。

你的state_params期待a:world_id被发送回来,所以添加一个隐藏字段将其发送回表格。

new.html.erb

<%= f.hidden_field :world_id, :value => @world.id %>

并将创建操作更新为

@world = World.find(params[:world_id])