Rails如何发送params [:commit]值

时间:2016-02-10 06:14:07

标签: ruby-on-rails

我正在构建一个博客应用程序,我正在尝试发送一些像

这样的值
  1. post.id
  2. params [:commit](value)

  3. 所以从我的show动作中我将这两个值发送到我的帖子控制器中的编辑操作,但我无法收集params [:commit]值..在我的情况下批准它会发送'批准'并且为了拒绝它将发送'拒绝'但它没有发送
    [帖/ show.html.erb]

    <div id="post_content">
        <h1 class="title"><%= @post.title %></h1>
        <p class="date">
            Submitted <%= time_ago_in_words(@post.created_at) %> Ago
            <% if user_signed_in? %>
             <%= link_to 'Edit', edit_post_path(@post) %> |
              <%= link_to 'Delete', post_path(@post), method: :delete, data: { confirm: 'Are you sure?' } %>
    
              <%= link_to "approve",[:edit,@post] ,commit: "Approve"%>
            <%= link_to "decline",[:edit,@post],commit: "decline" %>
    
            <% end %>
        </p>
        <p class="body"><%= @post.body %></p>
    
        <div id="comments">
            <h2><%= @post.comments.count %> Comments</h2>
            <%= render @post.comments %>
    
            <h3>Add a comment:</h3>
            <%= render "comments/form" %>
        </div>
    </div>
    


    [posts_controller.rb]

    class PostsController < ApplicationController
        before_action :authenticate_user! 
    
        def index
            @posts = Post.user_post(current_user).order('created_at DESC').paginate(:page => params[:page], :per_page => 5)
        end 
    
        def new
            @post = Post.new
        end
    
        def show    
            @post=find_params
        end
    
        def create
            @post = Post.new(post_params)           
            @post.user = current_user
            if @post.save
                Post.upload(params[:post][:files],@post.id)         
                redirect_to @post
            else
                render 'new'
            end
        end
    
        def edit
            @post = find_params
            puts "cccccccccc#{params[:commit]}"
            Post.up(@post.id,params[:commit])
        end
    
        def update
            @post = find_params
    
            if @post.update(post_params)
                redirect_to @post
            else
                render 'edit'
            end
        end
    
        def destroy
            @post = find_params
            @post.destroy
            redirect_to posts_path
        end
    
        private
            def post_params
                params.require(:post).permit(:title, :body)
            end
    
            def find_params
                Post.find(params[:id])
            end
    
    end
    

1 个答案:

答案 0 :(得分:1)

根据此更改您的link_to标记。

    <%= link_to "approve", edit_post_path(@post, commit: "Approve") %>
    <%= link_to "decline", edit_post_path(@post, commit: "Decline" %>