2017-05-22 3 views
1

Ich habe ein World Elternobjekt und ein Kind State-Objekt. Ich versuche, ein neues State-Objekt zu erstellen, und rails findet die ID der Welt nicht. Ich versuche, von der Weltshow-Seite auf das neue Statusformular zu verlinken, und die URL zeigt .../worlds/1/states/new, also warum greift das nicht auf die Eltern-ID zu? Der Fehler stammt angeblich von dieser Leitung in der Steuerung @world = World.find(params[:id]). Ich habe versucht, (params[:world_id]) sogar zu verwenden.Konnte nicht finden Welt mit 'ID' =

Der Kürze halber schreibe ich nur den relevanten Code hier.

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 

Der Link zum neuen Staatsform in Welten/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 

Staaten/_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> 

Ergebnisse Schienen Konsole, wenn Link 'New State' klicken

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) 

Antwort

2

Ändern Sie bitte Ihre link_to Helfer angeben und sagen Sie Rails, was der Parameter ist, den Sie senden:

Von:

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

An:

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

id, weil Sie die World durch :id als param zu finden versuchen.

auch versuchen, die param ändern, die innerhalb der Steuerung empfangen hat wird, wo Sie die @world Variable sind Einstellung:

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

Im show.html.erb:

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

aktualisieren: Was wir gemacht:

In der app/views/worlds/show.html.erb zu ändern e die Art und Weise der param gefassten wurde:

Von:

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

An:

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

Im /app/views/states/_form.html.erb die world_id als hidden_field hinzuzufügen:

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

Und dann in app/controllers/states_controller.rb die w zu ändern ay die params empfangen wurden:

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

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

Ich erhalte keine Route Streichhölzer '' '{: action => "neu",: controller =>“ states ",: id => # ddonche

+0

können Sie die in der Rails - Konsole angezeigten Parameter posten, wenn Sie auf den Link "New State" klicken. – fanta

+0

Ich habe auch versucht, '' '<% = link_to 'New State', new_world_state_path (@world_id)%>' '' und '' '... path (@ world.world_id)' '' zu verwenden, aber das sind beides sehr falsch. – ddonche

2

Die world_id während es an die weitergegeben wird: neue Aktion, kann es nicht wieder auf dem Formular an die erstellen Aktion übergeben werden.

Ihr state_params erwartet a: world_id wird zurückgesendet, fügen Sie also ein verstecktes Feld hinzu, um es zurück auf das Formular zu senden.

new.html.erb

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

und die Aktion schaffen aktualisieren, um

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

Das brachte mich an der neuen Aktion vorbei und zeigte schließlich das Formular, aber nachdem ich das Formular abgeschickt habe, bekomme ich den gleichen Fehler. '' 'Konnte World nicht mit 'id' =' '' finden – ddonche

Verwandte Themen