Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

update field from a link_to in rails 4

I just need a link_to link that automatically updates a value. I have done the following:

1) Added the field i want to update into the post_controller permit list:

#post_controller.rb
def post_params
      params.require(:post).permit(:title, :description, :file, :user_id, :category_id, :revisor_id, :visible)
    end

2) Added the link_to link in the view:

<td><%= link_to 'Publish', post_path(post, visible: true), method: :put %></td>

any idea what im doing wrong?

#routes.rb
resources :posts do
    get 'revisions', on: :collection
  end

the error i'm getting is:

param is missing or the value is empty: post

Here is the controller:

#posts_controller.rb
class PostsController < ApplicationController
  before_action :set_post, only: [:show, :edit, :update, :destroy]

  # GET /posts
  # GET /posts.json
  def index
    @posts = Post.all
  end

  def revisions
    @posts = Post.where(revisor_id: current_user.id)
  end

  # GET /posts/1
  # GET /posts/1.json
  def show
  end

  # GET /posts/new
  def new
    @post = Post.new
  end

  # GET /posts/1/edit
  def edit
  end

  # POST /posts
  # POST /posts.json
  def create
    @post = Post.new(post_params)
    @post.visible = true
    @post.user_id = current_user.id
    @petition = Petition.new(user_id: current_user.id, category_id: @post.category_id, status: "New")
    @petition.save
    respond_to do |format|
      if @post.save
        format.html { redirect_to @post, notice: 'Post was successfully created.' }
        format.json { render :show, status: :created, location: @post }
      else
        format.html { render :new }
        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /posts/1
  # PATCH/PUT /posts/1.json
  def update
    respond_to do |format|
      if @post.update(post_params)
        format.html { redirect_to @post, notice: 'Post was successfully updated.' }
        format.json { render :show, status: :ok, location: @post }
      else
        format.html { render :edit }
        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /posts/1
  # DELETE /posts/1.json
  def destroy
    @post.destroy
    respond_to do |format|
      format.html { redirect_to posts_url, notice: 'Post was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_post
      @post = Post.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def post_params
      params.require(:post).permit(:title, :description, :file, :user_id, :category_id, :revisor_id, :visible)
    end
end

Here is the view:

#revisions.html.erb
<h1>Pending for revision</h1>
<table>
  <thead>
  <tr>
    <th>Title</th>
    <th>Description</th>
    <th>User</th>
    <th>Category</th>
    <th>Revisor</th>
    <th colspan="3"></th>
  </tr>
  </thead>

  <tbody>
  <% @posts.pending_posts.each do |post| %>
      <tr>
        <td><%= post.title %></td>
        <td><%= post.description %></td>
        <td><%= post.user_id %></td>
        <td><%= post.category_id %></td>
        <% if !post.revisor_id %>
            <td> No asignado </td>
        <% else %>
            <td><%= post.revisor_id %></td>
        <% end %>
        <td><%= link_to 'Publish', post_path(post, visible: true), method: :put %></td>
        <!--<td><%#= link_to 'Edit', edit_petition_path(petition) %></td>-->
        <!--<td><%#= link_to 'Destroy', petition, method: :delete, data: { confirm: 'Are you sure?' } %></td>-->
      </tr>
  <% end %>
  </tbody>
</table>

Here is the model:

#post.rb
class Post < ActiveRecord::Base
  belongs_to :category
  belongs_to :user
  has_one :petition

  scope :my_revisions, ->(user){ where("posts.user_id = ?", user.id)}
  scope :visible_posts, -> {where(visible: true)}
  scope :pending_posts, -> {where(visible: false)}
end

SERVER LOG:

Started PUT "/posts/12?visible=true" for 127.0.0.1 at 2014-07-16 10:07:31 +0100
Processing by PostsController#update as HTML
  Parameters: {"authenticity_token"=>"authenticitytoken=", "visible"=>"true", "id"=>"12"}
  Post Load (0.2ms)  SELECT  "posts".* FROM "posts"  WHERE "posts"."id" = ? LIMIT 1  [["id", 12]]
Completed 400 Bad Request in 2ms

ActionController::ParameterMissing (param is missing or the value is empty: post):
  app/controllers/posts_controller.rb:79:in `post_params'
  app/controllers/posts_controller.rb:51:in `block in update'
  app/controllers/posts_controller.rb:50:in `update'


  Rendered /home/kbs23/.rvm/gems/ruby-2.1.0@global/gems/actionpack-4.1.2/lib/action_dispatch/middleware/templates/rescues/_source.erb (0.6ms)
  Rendered /home/kbs23/.rvm/gems/ruby-2.1.0@global/gems/actionpack-4.1.2/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.5ms)
  Rendered /home/kbs23/.rvm/gems/ruby-2.1.0@global/gems/actionpack-4.1.2/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.0ms)
  Rendered /home/kbs23/.rvm/gems/ruby-2.1.0@global/gems/actionpack-4.1.2/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (13.7ms)

Here are my routes:

#rake routes
         revisions_posts GET    /posts/revisions(.:format)     posts#revisions
                   posts GET    /posts(.:format)               posts#index
                         POST   /posts(.:format)               posts#create
                new_post GET    /posts/new(.:format)           posts#new
               edit_post GET    /posts/:id/edit(.:format)      posts#edit
                    post GET    /posts/:id(.:format)           posts#show
                         PATCH  /posts/:id(.:format)           posts#update
                         PUT    /posts/:id(.:format)           posts#update
                         DELETE /posts/:id(.:format)           posts#destroy
like image 332
user2129353 Avatar asked Dec 15 '22 21:12

user2129353


1 Answers

I finally got to fix this by just changing the link from:

<td><%= link_to 'Publish', post_path(post, visible: true), method: :put %></td>

to this:

<td><%= link_to 'Publish', post_path(post, post: {visible: :true}), method: :put %></td>
like image 170
user2129353 Avatar answered Jan 02 '23 16:01

user2129353