【问题标题】:How do I submit this form in Rails so it lands at /users/:id?如何在 Rails 中提交此表单,使其位于 /users/:id?
【发布时间】:2012-05-08 16:32:39
【问题描述】:

我正在尝试做一些在概念上看起来很简单的事情,但我就是无法让它发挥作用。这是我正在尝试做的事情:

我在 /users/index 页面上有一个简单的“搜索”表单。它 利用 jQuery Tokeninput 自动完成用户(名称/用户名) 当当前用户输入搜索字段时。我想做的事 是让用户键入一个名称,从列表中选择一个用户,然后单击 “提交”并被带到所选用户的个人资料(/users/:id/ - 这是 用户#show)。我已将 jQuery Tokeinput 配置为将 user_id 提交为:user_token。

我似乎无法正常工作。自动完成部分工作正常,但我不知道如何“提交”以便显示输入的用户个人资料。

当我点击表单上的“提交”按钮时会发生以下情况(从终端的开发日志中提取):

Started PUT "/users/2" for 127.0.0.1 at 2012-05-08 11:19:56 -0400
  Processing by UsersController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"blah blah=", "user"=>{"user_token"=>"41"}, "commit"=>"Go to profile", "id"=>"2"}
  User Load (0.3ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 2 LIMIT 1
  User Load (0.4ms)  SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", "2"]]
  CACHE (0.0ms)  SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1
Completed 500 Internal Server Error in 83ms

NoMethodError (undefined method `downcase!' for nil:NilClass):
  app/controllers/users_controller.rb:119:in `update'

所以它在用户控制器上调用更新操作,我假设因为“@user”已经存在(具体来说,是 current_user 点击提交按钮)。

在屏幕上,我看到了:

顶部显示的路径是 .../users/2(当前用户的 id),并且在 我看到的浏览器:

UsersController#update 中的 NoMethodError

未定义的方法“小写!”对于 nil:NilClass

我得到这个是因为它试图在用户控制器中运行“更新”操作,并且有一个“小写!”在更新操作开始时调用其中一个参数。该参数 ([:user][:email]) 显然不存在,因为它不在我提交的表单中。

我真正想做的是转到“/users/41”(其 id 作为 params[:user][:user_token] 传递的用户的显示页面)。我该怎么做?

以下是所有相关代码:

#users_controller.rb#Index

def index
  @title = "All users"
  @label = "All users"
  @list_users = User.order(:name).page(params[:page]) #generates users shown on index page
  @user = current_user

  # This is used to populate the autocomplete field in the little search form
  @users = User.where("LOWER(name) like ? OR LOWER(username) like ?", "%#{params[:q].downcase}%", "%#{params[:q].downcase}%").order('name ASC').limit(10) if params[:q]

  respond_to do |format|
    format.html # index.html.erb
    format.json { render json: @users, :only => [:id, :name, :username] }
  end
end

我的路线...

#routes.rb 
resources :comments
resources :invitations
resources :sessions, :only => [:new, :create, :destroy]
resources :shares, :controller => "item_shares", :as => "item_shares" do
  resources :comments
end
resources :posts, :controller => "item_posts", :as => "item_posts" do
  resources :comments
end
resources :items
resources :relationships, only: [:create, :destroy]
resources :users do
  member do
    get :following, :followers
  end
end
resources :password_resets

match '/signup',  :to => 'users#new'
match '/signup/:invitation_token' => 'users#new', :as => :signup_with_invitation
match '/signin',  :to => 'sessions#new'
match '/signout', :to => 'sessions#destroy'
match '/invite',  :to => 'invitations#new'

match '/users/:id/shared', :to => 'users#shared'
match '/users/:id/received', :to => 'users#received'
match '/users/:id/saved', :to => 'users#saved'
match '/users/:id/posts', :to => 'users#posts'
match '/reciprocal_followers', :to => 'users#reciprocal_followers' 

root :to => 'pages#home'

这是我的表单(这绝对行不通,尽管 jQuery Tokeninput 确实有效):

#_user_search_form.html.erb
<div class="form">
<span class="form-label-right round-bottom-left-5 round-top-right-5 gray-gradient">Find someone</span>
<%= form_for @user, :action => "show" do |f| %>

    <div class="field">
        <%= f.label :user_token, "Name or Username" %></br>
        <%= f.text_field :user_token, :placeholder => 'John Doe or JohnDoe123', "data-pre" =>  (@pre_populate_data.to_json(:only => [:id, :name, :username]) unless @pre_populate_data.nil?)  %>
    </div>

    <div class="actions">
        <%= f.submit "Go to profile" %>
    </div>
<% end %>
</div>

这是我的用户模型的相关部分:

#user.rb
attr_accessible  :user_token

attr_reader :user_token

【问题讨论】:

    标签: forms ruby-on-rails-3.1 routes jquery-tokeninput


    【解决方案1】:
    <% form_for @project, :url => { :controller => "project", :action => "thumbnail_save" } do |form| %>
    ....
    <% end %>
    

    在你的配置文件中也写这样的东西(放在顶部)

    get "users/show"
    

    如果这不起作用,试试这个。 (您可能需要更改您的表格才能在下面工作)

    match "project/thumbnail_save", :to => "project#thumbnail_save"
    

    如果还有问题,请告诉我们。

    【讨论】:

      猜你喜欢
      • 2014-04-09
      • 1970-01-01
      • 1970-01-01
      • 2019-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多