我搜索了很多,发现了类似的问题,但是任何看似合适,或者我都不理解。
我有3个型号,我选择一个型号的坏名称,即电视节目,你可能会将Show模型与show方法和视图混淆。
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
我为每个模型制作表格,我想做的是自动设置每个模型的外键。因此,在我的节目中,我想要创建一个将存储在该节目中的季节。
:显示/ 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><%=@show.id%></h1>
<h1><%=@show.episodes.count%></h1>
<h1></h1>
**<%= link_to 'create seasons', season_new_path(@show.id) %>**
<%= link_to 'Edit', edit_show_path(@show) %> |
<%= link_to 'Back', shows_path %>
在最后一行中,我传递了@ show.id来创建一个赛季,我希望使用@ show.id作为我的外键:show_id用于该赛季。我如何在我的参数中使用该ID?
我看了一些帖子,我试过这个,但是没有用,继承人控制器(只有我觉得有用的部分。) 的 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
修改 我的路线是: 的的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
我的表格是: 的季节/ 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 %>
显示/ 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 %>
编辑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
迁移
class AddUserIdtoShow < ActiveRecord::Migration
def change
add_index :shows , :user_id
end
end