Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 从Ruby on Rails上的隐藏字段设置对象属性_Ruby On Rails_Ruby_Forms_Ruby On Rails 4 - Fatal编程技术网

Ruby on rails 从Ruby on Rails上的隐藏字段设置对象属性

Ruby on rails 从Ruby on Rails上的隐藏字段设置对象属性,ruby-on-rails,ruby,forms,ruby-on-rails-4,Ruby On Rails,Ruby,Forms,Ruby On Rails 4,我试图通过一个隐藏的表单字段更新一个Idea属性challenge\u id。以下是字段: @challenge.id%> 当创意被创建时,它成功地将质询id作为参数传递给创意控制器#创建方法: 于2015-06-18 15:39:49-0400开始为::1发布“/ideas.js” IdeasController#创建为JS进行处理 参数:{“utf8”=>“✓", "idea“=>{”title“=>”adsf“,”description“=>”asf“,”域令牌“=>”20“,”挑战id“

我试图通过一个隐藏的表单字段更新一个
Idea
属性
challenge\u id
。以下是字段:

@challenge.id%>

当创意被创建时,它成功地将质询id作为参数传递给创意控制器#创建方法:

于2015-06-18 15:39:49-0400开始为::1发布“/ideas.js”
IdeasController#创建为JS进行处理
参数:{“utf8”=>“✓", "idea“=>{”title“=>”adsf“,”description“=>”asf“,”域令牌“=>”20“,”挑战id“=>”5“,”提交“=>”创建idea“}

这个
挑战\u id=>5
应该保存到下面行
@idea=idea.new(idea\u参数)
中的idea中:

ideas\u controller.rb

def create
    @idea = Idea.new(idea_params)

    respond_to do |format|
      if @idea.save
        idea_count = @idea.user.ideas_created_count
        @idea.user.update(:ideas_created_count => idea_count + 1)
        @idea.domains.each do |domain|
          current_user.add_points(1, category: domain.title)
        end
        @ideas = current_user.current_team.ideas.sort_by{|i| i.heat_index}.reverse
        @ideas = @ideas.paginate(:page => params[:ideas_page], :per_page => 10)
        format.html { redirect_to :back, notice: 'Idea was successfully created.' }
        format.json { render :show, status: :created, location: @idea }
        format.js
      else
        format.html { redirect_to :back, notice: "You must attach domains to your Idea." }
        format.json { render json: @idea.errors, status: :unprocessable_entity }
        format.js { render :create_failed }
      end
    end
  end
  .
  .
  def idea_params
   params.require(:idea).permit(:title, :description, :challenge_id)
  end

但是,此质询id未与其他允许的
idea\u参数
:title
:description
一起保存到
idea
。创建Idea时,如何将
challenge\u id
保存到Idea中?

我假设Idea表中有challenge\u id列

尝试以下方法:

def create
    @idea = Idea.new(idea_params)
    @idea.challenge_id = params[:idea][:challenge_id]
     #...
 end

如果要将其保存到的参数或列不同,请确保进行了更改,但您已经明白了这一点。

为什么不在表单中传递质询id,而不是使用隐藏字段?否则,用户可以在隐藏字段中输入任何他们想要的内容

表格:

然后:

def create
    @challenge = Challenge.find(params[:challenge_id])
    @idea = Idea.new(idea_params)
    @idea.challenge_id = @challenge.id
end

什么
将params
放在这个方法的第一行将被打印?还有什么
idea\u params
正在返回。
def create
    @challenge = Challenge.find(params[:challenge_id])
    @idea = Idea.new(idea_params)
    @idea.challenge_id = @challenge.id
end