无法捕获从视图到控制器的变量传递? Rails 4

时间:2014-10-18 08:21:03

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

我试图将视图中的变量传递给控制器​​。但是,即使我包括,控制器参数也不包括参数。有人可以帮我查一下吗? 感谢

show.html.erb

    <div class="col-xs-8">
    <p id="notice"><%= notice %></p>

    <p>
        <%= image_tag @post.avatar_url(:thumb).to_s %>
        <br>
      <strong>Tag:</strong>

      <%= @post.tag %>

    </p>
    <%= button_to "Add New Version", versions_path(:pid => "123"), :class => "btn btn-default", :method => :get %>
    <%= link_to 'Edit', edit_post_path(@post) %> |
    <%= link_to 'Back', posts_path %>

</div>  

我尝试在此网页中点击添加新版本...它将转到版本#index

所以这是我的版本索引。

class VersionsController < ApplicationController
  layout "loginpage"
  #before_action :set_version, only: [:show, :edit, :update, :destroy]

  def index
    @heading = "Add new version"
    @effect = Effect.new
    p "hello world"
    p params
    p params[:pid]
    p @effect
    p "hello owlrd"

我试图打印出params,看看是否存在pid param,继承人输出

Started GET "/versions" for 127.0.0.1 at 2014-10-18 19:17:28 +1100
Processing by VersionsController#index as HTML
"hello world"
{"action"=>"index", "controller"=>"versions"}
nil
#<Effect id: nil, effect1: nil, effect2: nil, effect3: nil, created_at: nil, updated_at: nil>
"hello owlrd"

似乎我无法将pid放入params。

的routes.rb

Rails.application.routes.draw do
  resources :trashes

  resources :versions

  resources :effects

  devise_for :users
  resources :posts
  resources :homes

  # routing to the login page
  devise_scope :user do
    root :to => 'devise/sessions#new'
  end

rake routes

                      Prefix Verb   URI Pattern                    Controller#Action
                 trashes GET    /trashes(.:format)             trashes#index
                         POST   /trashes(.:format)             trashes#create
               new_trash GET    /trashes/new(.:format)         trashes#new
              edit_trash GET    /trashes/:id/edit(.:format)    trashes#edit
                   trash GET    /trashes/:id(.:format)         trashes#show
                         PATCH  /trashes/:id(.:format)         trashes#update
                         PUT    /trashes/:id(.:format)         trashes#update
                         DELETE /trashes/:id(.:format)         trashes#destroy
                versions GET    /versions(.:format)            versions#index
                         POST   /versions(.:format)            versions#create
             new_version GET    /versions/new(.:format)        versions#new
            edit_version GET    /versions/:id/edit(.:format)   versions#edit
                 version GET    /versions/:id(.:format)        versions#show
                         PATCH  /versions/:id(.:format)        versions#update
                         PUT    /versions/:id(.:format)        versions#update
                         DELETE /versions/:id(.:format)        versions#destroy
                 effects GET    /effects(.:format)             effects#index
                         POST   /effects(.:format)             effects#create
              new_effect GET    /effects/new(.:format)         effects#new
             edit_effect GET    /effects/:id/edit(.:format)    effects#edit
                  effect GET    /effects/:id(.:format)         effects#show
                         PATCH  /effects/:id(.:format)         effects#update
                         PUT    /effects/:id(.:format)         effects#update
                         DELETE /effects/:id(.:format)         effects#destroy
        new_user_session GET    /users/sign_in(.:format)       devise/sessions#new
            user_session POST   /users/sign_in(.:format)       devise/sessions#create
    destroy_user_session DELETE /users/sign_out(.:format)      devise/sessions#destroy
           user_password POST   /users/password(.:format)      devise/passwords#create
       new_user_password GET    /users/password/new(.:format)  devise/passwords#new
      edit_user_password GET    /users/password/edit(.:format) devise/passwords#edit
                         PATCH  /users/password(.:format)      devise/passwords#update
                         PUT    /users/password(.:format)      devise/passwords#update
cancel_user_registration GET    /users/cancel(.:format)        devise/registrations#cancel
       user_registration POST   /users(.:format)               devise/registrations#create
   new_user_registration GET    /users/sign_up(.:format)       devise/registrations#new
  edit_user_registration GET    /users/edit(.:format)          devise/registrations#edit
                         PATCH  /users(.:format)               devise/registrations#update
                         PUT    /users(.:format)               devise/registrations#update
                         DELETE /users(.:format)               devise/registrations#destroy
                   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
                   homes GET    /homes(.:format)               homes#index
                         POST   /homes(.:format)               homes#create
                new_home GET    /homes/new(.:format)           homes#new
               edit_home GET    /homes/:id/edit(.:format)      homes#edit
                    home GET    /homes/:id(.:format)           homes#show
                         PATCH  /homes/:id(.:format)           homes#update
                         PUT    /homes/:id(.:format)           homes#update
                         DELETE /homes/:id(.:format)           homes#destroy
                    root GET    /                              devise/sessions#new

1 个答案:

答案 0 :(得分:0)

问题不在于您的路线,而在于您正在使用的帮助器。如果您查看由button_to生成的html,它将是这样的:

<%= button_to "New", action: "new" %>
# => "<form method="post" action="/controller/new" class="button_to">
#      <div><input value="New" type="submit" /></div>
#    </form>"

所以它为它创建了一个表单,默认情况下表单 发送帖子请求

<强> FIX:

将button_to帮助器更改为link_to ,默认情况下会发送一个get请求,因此您不必为其指定方法选项

<%= link_to "Add New Version", versions_path(:pid => "123"), :class => "btn btn-default", :method => :get %>