AnApprentice
AnApprentice

Reputation: 110950

Rails - How to build a link_to that PUTs to the Controller's update

I have the controller:

class GroupRequestsController < ApplicationController

  def update
    @group_request = GroupRequest.find(params[:id])

    respond_to do |format|
      if @group_request.update_attributes(params[:group_request])
        format.js
      else
        format.js
      end
    end
  end

In my view I have:

<button type="button" class="button positive tiny">Add</button>
<button type="button" class="button tiny">Ignore</button>

How can I make those buttons on click, make a remote call to the controller's update method with either add or ignore as the decision param's?

Thanks

Upvotes: 1

Views: 188

Answers (1)

jesse reiss
jesse reiss

Reputation: 4579

Use separate forms.

-form_for :group_request, :remote => true do |f|
  =f.hidden_field :decision, "Add"
  %button{:type => "button", :class => "..."} Add

-form_for :group_request, :remote => true do |f|
  =f.hidden_field :decision, "Ignore"
  %button{:type => "button", :class => "..."} Ignore

I use HAML syntax because if you aren't using HAML you should. This is the only consistent way to send values. The Button tag has a "value" attribute, but it isn't supported in all major browsers so you can't rely on it. Unfortunately it means you have to do a bunch of annoying styling to get the buttons to display next to each other.

The only other option I can think of is to use pure JS to make the buttons act like forms, but I can't recommend that because it doesn't degrade gracefully.

Upvotes: 1

Related Questions