I have a form that lets users add a new blocked tv show to their list of blocked shows. The form is not taking the param values (:user_id
, :title
, :image
) that I tried to set in the controller. I'm a beginner, so I'm guessing the syntax is the problem.
我有一个表单,允许用户将新的被屏蔽的电视节目添加到他们的被屏蔽节目列表中。表单没有采用我试图在控制器中设置的参数值(:user_id,:title,:image)。我是初学者,所以我猜测语法就是问题。
Also I am getting a Couldn't find Tvshow without Id
error when trying to use the @tvshow instance variable to set the param values of :title
and :image
. Each Blocked show should have the same title and image as the tvshow that the user selects in the collection_select
. Is there an easier way to do this?
当我尝试使用@tvshow实例变量来设置参数值:title和:image时,我得到一个无法找到Tvshow而没有Id错误。每个Blocked节目应具有与用户在collection_select中选择的tvshow相同的标题和图像。有更简单的方法吗?
View
视图
<%= form_for @blockedshow do |b| %>
<%= b.label :tvshow_id, "Add a Blocked TV Show " %><br/>
<%= collection_select(:blockedshow, :tvshow_id, Tvshow.all, :id, :title, prompt: true) %>
<%= submit_tag 'Add' %>
<% end %>
Controller
调节器
class BlockedshowsController < ApplicationController
def new
@blockedshow = Blockedshow.new
end
def create
@tvshow = Tvshow.find params[:blockedshow][:id]
@blockedshow = Blockedshow.new(safe_blockedshow)
params[:user_id] = current_user.id
params[:title] = @tvshow.title
params[:image] = @tvshow.image
if @blockedshow.save
flash[:notice] = "New Blocked TV Show added successfully"
redirect_to tv_show_index_path
else
render 'new'
end
end
private
def safe_blockedshow
params.require(:blockedshow).permit(:title, :user_id, :tvshow_id, :image)
end
end
Blockedshow model
Blockedshow模型
class Blockedshow < ActiveRecord::Base
has_many :phrases
has_many :tvshows
belongs_to :user
end
Tvshow model
Tvshow模型
class Tvshow < ActiveRecord::Base
has_many :phrases
belongs_to :blockedshow
def self.search_for (query)
where('title LIKE :query', query: "%#{query}%")
end
end
Routes
路线
resources :blockedshows
post 'blockedshows', to:'blockedshows#create#[:id]'
2 个解决方案
#1
0
you are getting the issue because params[:blockedshow][:id] is not passed, if your trying to access the Tvshow id selected by from the drop-list you can do the following
你得到的问题是因为没有传递params [:blockedshow] [:id],如果你试图访问从下拉列表中选择的Tvshow id,你可以执行以下操作
@tvshow = Tvshow.find params[:blockedshow][:tvshow_id]
@tvshow = Tvshow.find params [:blockedshow] [:tvshow_id]
#2
0
Just fixed by changing the controller to this:
只需将控制器更改为:
def create
@tvshow = Tvshow.find params[:blockedshow][:tvshow_id]
@blockedshow = Blockedshow.new(
:user_id =>current_user.id,
:title=> @tvshow.title,
:image=> @tvshow.image,
:tvshow_id=>@tvshow.id
)
#1
0
you are getting the issue because params[:blockedshow][:id] is not passed, if your trying to access the Tvshow id selected by from the drop-list you can do the following
你得到的问题是因为没有传递params [:blockedshow] [:id],如果你试图访问从下拉列表中选择的Tvshow id,你可以执行以下操作
@tvshow = Tvshow.find params[:blockedshow][:tvshow_id]
@tvshow = Tvshow.find params [:blockedshow] [:tvshow_id]
#2
0
Just fixed by changing the controller to this:
只需将控制器更改为:
def create
@tvshow = Tvshow.find params[:blockedshow][:tvshow_id]
@blockedshow = Blockedshow.new(
:user_id =>current_user.id,
:title=> @tvshow.title,
:image=> @tvshow.image,
:tvshow_id=>@tvshow.id
)