从rails 4中的link_to更新字段

时间:2014-07-15 15:34:14

标签: ruby-on-rails ruby-on-rails-4 field

我只需要一个自动更新值的link_to链接。 我做了以下事情:

1)将我要更新的字段添加到post_controller许可列表中:

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

2)在视图中添加了link_to链接:

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

知道我做错了什么?

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

我得到的错误是:

param is missing or the value is empty: post

这是控制器:

#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

以下是观点:

#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>

以下是模型:

#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

服务器日志:

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)

以下是我的路线:

#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

1 个答案:

答案 0 :(得分:12)

我终于通过改变链接来解决这个问题:

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

到此:

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