0

I wanted to know how to go about have a submit button that will change a boolean attribute, instead of using radio buttons.

As a example if I display a list of 'published' and unpublished' article posts on the Post#index page and I wanted a button called 'Publish' that sets the :is_published field on the Post model to true.

I'm using strong_parameters on Rails 3.2.13

I was thinking in the Post controller I would have

def index
  @post = Post.recent
end

def update
  @post = Post.find_by_slug(params[:id])
  if params[:publish_button]
    @post.is_published = true
    @post.save
    redirect_to root_path
  end
end

private

 def post_params
   params.require(:person).permit(:body, :publish_button)
 end

In my Post#index view I have a form_for that has <%= f.submit 'Publish', name: publish_button %>

Here is the view in Post#index

<%= form_for @post do |f| %>
  <div class="field">
    <%= f.label :body %><br />
    <%= f.text_field :body %>
  </div>
  <div class="actions">
    <%= f.submit %>
    <%= f.submit_tag 'Publish Post', name: 'publish_post' %>
  </div>
<% end %>

The simple model follows

class Post < ActiveRecord::Base
  include ActiveModel::ForbiddenAttributesProtection

  scope :recent, -> { order('updated_at DESC') }
end

But I'm getting an error of Required parameter missing: post

Thanks in advance.

Update I've added a model and view that corresponds to the question. I hope it helps.

Wasabi Developer
  • 3,523
  • 6
  • 36
  • 60

1 Answers1

0

pass in your values as hidden fields, e.g.

= form_for @post do |f|
  = f.hidden_field :is_published, value: "1"
  = f.submit "Publish"

and if you want it to be inline like a button_to, give it the button_to class:

= form_for current_user.profile, html: { class: 'button_to' } do |f|
  ...
schpet
  • 9,664
  • 6
  • 32
  • 35