繁体   English   中英

投票按钮,是和否。Rails 4

[英]Voting buttons, Yes and No. Rails 4

嗨,我正在向我的网站添加投票。 用户可以投票“是”或“否”。我填写了表格,但仅对“是”按钮返回了错误

<%= form_for([@post, @post.votes.build]) do |c| %>
            <%= c.input value: 1, type: :hidden %>
            <%= c.submit :Yes, class: "btn btn-large btn-primary" %>
        <% end %>

这是错误: 很抱歉,但是出了点问题。

从理论上讲,我想做这样的事情:

<div class="text-success">
    Votează inițiativa<br>
    <button class="btn btn-large btn-primary">PRO</button> sau 
    <button class="btn btn-danger">CONTRA</button>
</div>

见我的控制器:

class VotesController < ApplicationController
    def new
    end

    def create
    @post = Post.find(params[:id])
        @votes = @post.votes.create(votes_params)
        @votes.user = current_user
        @votes.save
    redirect_to @votes
  end

    private

    def votes_params
        votes_params = params.require(:votes).permit(:stare)
    end
end

查看我的模型:

class Vote < ActiveRecord::Base
    belongs_to :user, dependent: :destroy
    belongs_to :post
end

我该怎么做呢?

您需要发布实际错误-通常通过输入heroku logs (听起来好像已经在生产环境中运行它了)

从我所看到的,我将尝试修复如下错误:

 <%= form_for([@post, @post.votes.build]) do |c| %>
      <%= c.input value: 1, type: :hidden %>
      <%= c.submit :Yes, class: "btn btn-large btn-primary" %>
  <% end %>


  def create
    @post = Post.find(params[:id])
        @votes = @post.votes.create(votes_params)
        @votes.save
    redirect_to @post
  end

  private

  def votes_params
    params.require(:post).permit(:stare).merge(user_id: current_user.id)
  end

我认为您的params hash格式会有些错误-您需要将其发布以帮助我们了解如何正确修复错误

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM