问题描述:

I've searched a lot , found similar questions , but any seem to fit , or i did not understood right.

i have 3 models , i choose a bad name for one model, that is for Tv Shows , and you may confuse the Show model with show method and view .

Show.rb

class Show < ActiveRecord::Base

has_many :seasons

belongs_to :user

has_many :episodes , through: :seasons

accepts_nested_attributes_for :seasons

attr_accessible :name,:status,:duration

end

Season.rb

class Season < ActiveRecord::Base

belongs_to :show

has_many :episodes

accepts_nested_attributes_for :episodes

attr_accessible :order,:status

end

Episode.rb

class Episode < ActiveRecord::Base

belongs_to :season

attr_accessible :number

end

My forms for each model are separeted, what i wanna do is set the foreigns keys for each one automatically. So , in the view of my Show i wanna create a season that will be stored in that Show.

Shows/show.html.erb

p id="notice"><%= notice %></p>

<% @seasons = Season.all %>

<% @episodes= Episode.all%>

<p>

<strong>Name:</strong>

<%= @show.name %>

</p>

<p>

<strong>Status:</strong>

<%= @show.status %>

</p>

<p>

<strong>Episode Duration:</strong>

<%= @show.Episode_Duration %>

</p>

<% @seasons.where(:show_id => @show.id).each do |season| %>

<tr>

<td><%= season.order %></td>

<td><%= season.status %></td>

<h3>episodios</h3>

<hr>

<% @episodes.where(:season_id => season.id).each do |episode| %>

<td> <%= episode.number%></td>

<%end%>

<hr>

<td><%= link_to 'episode', episode_new_path(season.id)%></td>

</tr>

<br>

<% end %>

<h1><%[email protected]%></h1>

<h1><%[email protected]%></h1>

<h1></h1>

**<%= link_to 'create seasons', season_new_path(@show.id) %>**

<%= link_to 'Edit', edit_show_path(@show) %> |

<%= link_to 'Back', shows_path %>

In that lasts line, i'm passing @show.id for create a season, i want use that @show.id as my foreign key :show_id for that season. how i use that id in my params?

I saw some posts and i tried this , but didn't worked , heres the controller(only the part that i think is useful.)

seasons_controller.rb

def create

@season = Season.new(season_params)

@season.show_id = params[:id]

@season.save

end

respond_to do |format|

if @season.save

format.html { redirect_to @season, notice: 'Season was successfully created.' }

format.json { render :show, status: :created, location: @season }

else

format.html { render :new }

format.json { render json: @season.errors, status: :unprocessable_entity }

end

end

def season_params

params.require(:season).permit(:order, :status)

end

EDIT

My routes are:

Routes.rb

devise_for :users

resources :episodes

resources :seasons

resources :shows

root 'shows#index'

get 'seasons/create/:id' => 'seasons#new', as: :season_new

get 'episodes/create/:id' => 'episodes#new', as: :episode_new

put 'subscribe_show/:id' => 'shows#subscribe', as: :user_subscribe_show

put 'unsubscribe_show/:id' => 'shows#unsubscribe', as: :user_unsubscribe_show

get 'user/dashboard/' => 'users#index', as: :user_dashboard

And my forms are:

seasons/form.html.erb

<%= form_for(@season) do |f| %>

<% if @season.errors.any? %>

<div id="error_explanation">

<h2><%= pluralize(@season.errors.count, "error") %> prohibited this season from being saved:</h2>

<ul>

<% @season.errors.full_messages.each do |message| %>

<li><%= message %></li>

<% end %>

</ul>

</div>

<% end %>

<div class="field">

<%= f.label :order %><br>

<%= f.number_field :order %>

</div>

<div class="field">

<%= f.label :status %><br>

<%= f.text_field :status %>

</div>

<div class="actions">

<%= f.submit %>

</div>

<% end %>

Show/form.html.erb

<%= form_for(@show) do |f| %>

<% if @show.errors.any? %>

<div id="error_explanation">

<h2><%= pluralize(@show.errors.count, "error") %> prohibited this show from being saved:</h2>

<ul>

<% @show.errors.full_messages.each do |message| %>

<li><%= message %></li>

<% end %>

</ul>

</div>

<% end %>

<div class="field">

<%= f.label :name %><br>

<%= f.text_field :name %>

</div>

<div class="field">

<%= f.label :status %><br>

<%= f.text_field :status %>

</div>

<div class="field">

<%= f.label :Episode_Duration %><br>

<%= f.text_field :Episode_Duration %>

</div>

<div class="actions">

<%= f.submit %>

</div>

<% end %>

EDIT 2:

schema.rb

create_table "episodes", force: :cascade do |t|

t.integer "number"

t.integer "season_id"

t.datetime "created_at", null: false

t.datetime "updated_at", null: false

end

create_table "seasons", force: :cascade do |t|

t.integer "order"

t.string "status"

t.integer "show_id"

t.datetime "created_at", null: false

t.datetime "updated_at", null: false

t.boolean "watched"

end

create_table "shows", force: :cascade do |t|

t.string "name"

t.string "status"

t.datetime "created_at", null: false

t.datetime "updated_at", null: false

t.integer "Episode_Duration"

t.integer "user_id"

end

Migrations

class AddUserIdtoShow < ActiveRecord::Migration

def change

add_index :shows , :user_id

end

end

相关阅读:
Top