Setting an object attribute from a hidden field Ruby on Rails -
i'm trying update idea
attribute challenge_id
through hidden form field. here field:
<%= f.hidden_field :challenge_id, :value => @challenge.id %>
it passes challenge id param when idea created idea controller#create method:
started post "/ideas.js" ::1 @ 2015-06-18 15:39:49 -0400 processing ideascontroller#create js parameters: {"utf8"=>"✓", "idea"=>{"title"=>"adsf", "description"=>"asf", "domain_tokens"=>"20", "challenge_id"=>"5"}, "commit"=>"create idea"}
this challenge_id => 5
should saved idea in line @idea = idea.new(idea_params)
below:
ideas_controller.rb
def create @idea = idea.new(idea_params) respond_to |format| if @idea.save idea_count = @idea.user.ideas_created_count @idea.user.update(:ideas_created_count => idea_count + 1) @idea.domains.each |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 created.' } format.json { render :show, status: :created, location: @idea } format.js else format.html { redirect_to :back, notice: "you must attach domains 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
however, challenge id isn't being saved idea
other permitted idea_params
, :title
, :description
. how can challenge_id
saved idea when it's created?
instead of using hidden field, why not pass in challenge_id in form? otherwise, leave open possibility users can enter whatever want in hidden field.
form:
form_for [@challenge, idea.new] |f|
and then:
def create @challenge = challenge.find(params[:challenge_id]) @idea = idea.new(idea_params) @idea.challenge_id = @challenge.id end
Comments
Post a Comment