ruby_rhod_on_rails
ruby_rhod_on_rails

Reputation: 105

rails 5 form_for (post method) issues get request

I tried every configuration of this form_for tag that would tell it to post the form but it keeps issuing a GET request with the parameters I need in the post request. Here is the form in the view:

dash.html.erb:

<ul class = "new-offer-form" id = "new-offer-form">
    <form class="form-inline">

    <%= form_for(@offer, url: offers_create_path(@offer), method: :post, :html => {:class => "form-inline"}) do |f|%>
        <%= render 'shared/error_messages', object: f.object %>
        <div class="form-group">
            <li class = "offer-item"> <%= f.text_field :category %> </li>
        </div>
        <div class="form-group">
            <li class = "offer-item"> <%= f.text_field :plan_name %> </li>
        </div>
        <div class="form-group">
            <li class = "offer-item"> <%= f.text_field :price %> </li>
        </div>
        <div class="form-group">
            <li class = "offer-item"> <%= f.text_field :rate %> </li>
        </div>
        <div class="form-group">
            <li class = "offer-item"> <%= f.submit "Add new service plan", id: "new_link" %> </li>
        </div>

    <% end %>
</form>

Here is the controller code:

def dash
    @offer = current_user.offers.build()
    @offers = current_user.offers
    respond_to do |format|
      format.html
      format.json {render :json => @offer}
end

end

def create
    @user = current_user
    @offer = @user.offers.build(offer_params)

    if @offer.save
       flash[:success] = "Service plan added"
    else
       render 'offers/dash'
   end
end

This is what appeared in the server log after submitting the form:

Started GET "/offers/dash?utf8=%E2%9C   %93&authenticity_token=42V3KL6EB9BBE4h7LQbMIR44MZLq2KisKtgA6gh9dMBer5tewe%2     BUEWzZ0t0MG49IdisczNPz0PMtnUHSAxdf3A%3D%3D&offer%5Bcategory%5D=test&    offer%5Bplan_name%5D=hello&offer%5Bprice%5D=there&offer%5Brate%5D=world&commit=Add+new+service+plan" for 127.0.0.1 at 2017-04-12 22:16:25 -0400
Processing by OffersController#dash as HTML
  Parameters: {"utf8"=>"✓",     "authenticity_token"=>"42V3KL6EB9BBE4h7LQbMIR44MZLq2KisKtgA6gh9dMBer5tewe+U    EWzZ0t0MG49IdisczNPz0PMtnUHSAxdf3A==", "offer"=>{"category"=>"test",     "plan_name"=>"hello", "price"=>"there", "rate"=>"world"}, "commit"=>"Add new service plan"}

I tried changing url to 'offers/create' which routes to 'offers#create' to no avail. Any ideas?

Upvotes: 1

Views: 1314

Answers (1)

Mark Swardstrom
Mark Swardstrom

Reputation: 18080

As Dario said in the comments, you are essentially putting in two <form> tags which is not valid HTML.

form_for will be a post by default (so you don't need :method => :post). A basic html <form> tag will be a get by default, that's why you are seeing a Get request.

And, if you are going to the standard create action and it's defined in routes.

resources :offers

Then this would be the form_for line

<%= form_for(@offer, :html => {:class => "form-inline"}) do |f|%>

Upvotes: 1

Related Questions