link_to将错误的参数传递给控制器

时间:2017-05-05 20:32:56

标签: ruby-on-rails

我的项目控制器中的set_project方法出错了。根据我看到的错误屏幕,传递的参数如下:

{" project_id" =>" 8"," id" =>" add_vote"}

我认为数字8应该是通过的。这是我的项目控制器:

class ProjectsController < ApplicationController
  before_action :set_project, only: [:show, :edit, :update, :destroy]
  before_action :set_all_votes, only: [:show, :update]

  # GET /projects
  # GET /projects.json
  def index
    @projects = Project.all
    @votes = Vote
  end

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

  # GET /projects/new
  def new
    @project = Project.new
  end

  # GET /projects/1/edit
  def edit
  end

  # POST /projects
  # POST /projects.json
  def create
    @project = Project.new(project_params)

    respond_to do |format|
      if @project.save
        format.html { redirect_to @project, notice: 'Project was successfully created.' }
        format.json { render :show, status: :created, location: @project }
      else
        format.html { render :new }
        format.json { render json: @project.errors, status: :unprocessable_entity }
      end
    end
  end

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

  # DELETE /projects/1
  # DELETE /projects/1.json
  def destroy
    @project.destroy
    respond_to do |format|
      format.html { redirect_to projects_url, notice: 'Project was successfully destroyed.' }
      format.json { head :no_content }
    end
  end


  def add_vote
    unless params[:project_id].nil?
      @project = Project.find(params[:project_id])
      @newVote = Vote.create!(vote_value: true, user_id: current_user.id, project_id: @project.id)
      respond_to do |format|
        format.html { redirect_to @project, notice: 'Your vote has been recorded'} 
        format.json { render :show, status: :ok, location: @project } 
        end
    end
  end


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

    def set_all_votes
      @votes = Vote.all
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def project_params
      params.require(:project).permit(:title, :video_url, :description, :images, :team_name, :vote_count)
    end
end

这是我的项目展示页面。点击&#34;为此项目投票&#34;按钮是触发错误的原因。

<p id="notice"><%= notice %></p>


<p>
  <strong>Title:</strong>
  <%= @project.title %>
</p>

<p>
  <strong>Video url:</strong>
  <%= @project.video_url %>
</p>



<iframe src='https://www.youtube.com/watch?v=HbW-Bnm6Ipg' frameborder='0' allowfullscreen></iframe>




<p>
  <strong>Description:</strong>
  <%= @project.description %>
</p>

<br><br>


<div class="voteButton">
  <%= link_to "<button>Vote for this project</button>".html_safe, add_vote_path(:project_id => @project.id) %>
</div>
<br><br>

<%= link_to 'Edit', edit_project_path(@project) %> |
<%= link_to 'Back', projects_path %>

这是我的路线档案:

Rails.application.routes.draw do
  devise_for :users
  resources :votes
  resources :projects
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
  root 'projects#index'


  controller :projects do 
    get '/projects/add_vote' => 'projects#add_vote', as: :add_vote
  end

end

1 个答案:

答案 0 :(得分:1)

如果您在控制台中输入rake routes,您会看到由于resources :projects您的/projects/:id路由/projects/add_vote实际上与模式匹配的路由。当新请求进入时,路由器将查找从rake路由列表的顶部到底部的路由匹配。要解决此问题,您可以将留置权移至resources :projects以下add_vote

另外,我认为更改add_vote路径以包含项目的ID会很有帮助:

get '/projects/:id/add_vote' => 'projects#add_vote', as: :add_vote

这将带来额外的好处,就是您当前的情况不匹配/projects/:id。您的链接将变为:

<%= link_to "<button>Vote for this project</button>".html_safe, add_vote_path(@project) %>
相关问题