문제

I'm using rails 4.0.1

<%= form_for @event, :html => { :multipart => true} do |f| %>
  <div class="field">
    <%= f.label :title %><br>
    <%= f.text_field :title %>
  </div>  
  <div class="field">
    <%= f.label :content %><br>
    <%= f.text_area :content %>
  </div>
  <div class="field">
    <%= f.label :place_id %><br>
    <%= f.collection_select(:place_id, @places, :id, :title) %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

And I want to check for current_user.id and Place.user_id (it stores creator id). In Events cotroller i'm trying to use:

def create
  @places = Place.all
  @event = Event.new(event_params)
  @event.user_id = current_user.id
  @curplace = Place.find_by(id: params[:place_id])
  @event.content = @curplace.id 
  respond_to do |format|
    if @event.save
      format.html { redirect_to @event, notice: 'Event was successfully created.' }
      format.json { render action: 'show', status: :created, location: @event }
    else
      format.html { render action: 'new' }
      format.json { render json: @event.errors, status: :unprocessable_entity }
    end
  end
end

But i got an error. I think i'm not getting this Place_id param right or anything else?

도움이 되었습니까?

해결책

Further to the comment from Ankush Kataria, the form_for helper basically creates a form which combines all the params into a hash, as opposed to form_tag, which just makes the params independently

As you've discovered, this means your params will be accessed by:

#form_for
params[:variable][:param]

#form_tag
params[:param]

form_for

The reason why this is important is because if you're using the RESTful routes interface, you'll be able to create / edit / update a variety of records

form_for basically keeps consistency throughout this process, pre-populating your forms with the various values, and keeping your code DRY

To call a form_for helper, you have to define the @varaible the form will populate. This @variable needs to be an ActiveRecord object, and is why you have to build it in the new action before your form shows


form_tag

form_tag is much more independent of the form_for helper, doesn't require any @variable, and creates the params individually

You'd use a form_tag for the likes of a contact us form or similar


Your Code

Your form looks good, but your create action can be dried up:

def create
  @places = Place.all
  @event = Event.new(event_params)

  respond_to do |format|
    if @event.save
      format.html { redirect_to @event, notice: 'Event was successfully created.' }
      format.json { render action: 'show', status: :created, location: @event }
    else
      format.html { render action: 'new' }
      format.json { render json: @event.errors, status: :unprocessable_entity }
    end
  end
end

private 
def event_params
    params.require(:event).permit(:title, :content).merge(user_id: current_user.id, place_id: params[:event][:place_id])
end

다른 팁

You are right that params[:place_id] isn't returning the value you expect. It only returns nil. To get the :place_id that's submitted by the form, you have to do this:

@curplace = Place.find(params[:event][:place_id])

Just replace the old line with the code above. It's because your form submits the data in the fields inside an :event key in the params hash since you're using the form_for helper method provided by Rails. That is its default behavior unless you change the 'name' attribute's value in the input fields.

Hope that helps!

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top