开发者

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

开发者 https://www.devze.com 2023-03-08 19:40 出处:网络
I have the controller: class GroupRequestsController < ApplicationController def update @group_request = GroupRequest.find(params[:id])

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
    开发者_如何学JAVA  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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消