我正在尝试为用户导入创建模式。模式从用户索引页面和所述页面上的按钮正常工作,但我想添加导航栏链接并从应用程序的任何位置打开它。
我已经询问并收到了类似于这个问题的答案,但是现在我正在处理我的routes.rb中的自定义控制器方法和集合,这在尝试在应用程序中的任何位置打开它时会导致我出现问题。
这是我的模态部分:
## layouts/_import_modal.html.erb
<!-- Modal -->
<div class="modal fade" id="userImport" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">Modal title</h4>
</div>
<%= form_tag import_users_path, multipart: true, remote: true, authenticity_token: true do %>
<div class="modal-body">
<div class="row">
<div class="col-xs-12">
<h4>Select File To Import</h4>
<div class="input-group">
<label class="input-group-btn">
<span class="btn btn-primary">
Browse…<input type="file" style="display: none;" multiple>
</span>
</label>
<input type="text" class="form-control" readonly>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<%= submit_tag "Import User List", :class => 'btn btn-primary' %>
</div>
<% end %>
</div>
</div>
</div>
我的控制器操作:
def import
User.import(params[:file].path)
redirect_to users_path, notice: 'Users Added Successfully'
respond_to do |format|
format.js
end
end
我的import.js.erb:
$("#userImport").modal('show');
我的模型方法:
def self.headers
{ "Email Address" => "email",
"First Name" => "f_name",
"Last Name" => "l_name",
"Telephone" => "primary_tel"
}
end
def self.import(file)
CSV.parse(self.parse_headers(file), headers: true) do |rows|
User.create! row.to_hash
end
end
def self.parse_headers (file)
File.open(file) { |f|
first_line = f.readline
self.headers.each { |k,v| first_line[k] &&= v }
first_line + f.read }
end
现在这是最奇怪的部分..当我点击导航栏中的链接时,服务器输出如下:
Started GET "/users/import" for ::1 at 2016-08-31 23:07:59 -0600
Processing by UsersController#show as JS
Parameters: {"id"=>"import"}
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 1], ["LIMIT", 1]]
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."user_ident" = $1 LIMIT $2 [["user_ident", "import"], ["LIMIT", 1]]
Rendering users/show.html.erb within layouts/application
Rendered users/show.html.erb within layouts/application (14.0ms)
Completed 500 Internal Server Error in 41ms (ActiveRecord: 0.5ms)
ActionView::Template::Error (undefined method `role' for nil:NilClass):
1: <p>
2: <strong>Role:</strong>
3: <%= @user.role.name %>
4: </p>
5:
6: <p>
app/views/users/show.html.erb:3:in `_app_views_users_show_html_erb__2032490711892636426_70270834135860'
Rendering /Users/developer/.rvm/gems/ruby-2.3.1/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/template_error.text.erb
Rendering /Users/developer/.rvm/gems/ruby-2.3.1/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_trace.text.erb
Rendered /Users/developer/.rvm/gems/ruby-2.3.1/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_trace.text.erb (0.5ms)
Rendering /Users/developer/.rvm/gems/ruby-2.3.1/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.text.erb
Rendered /Users/developer/.rvm/gems/ruby-2.3.1/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.text.erb (0.6ms)
Rendered /Users/developer/.rvm/gems/ruby-2.3.1/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/template_error.text.erb (30.8ms)
DEPRECATION WARNING: #original_exception is deprecated. Use #cause instead. (called from status_code_with_paginate at /Users/developer/.rvm/gems/ruby-2.3.1/gems/will_paginate-3.1.0/lib/will_paginate/railtie.rb:49)
编辑#1 - 添加用户路线输出
import_users POST /users/import(.:format) users#import
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
编辑#2 - 添加新的link_to和服务器错误输出
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 1], ["LIMIT", 1]]
Completed 500 Internal Server Error in 3ms (ActiveRecord: 0.5ms)
TypeError (no implicit conversion of nil into String):
app/models/user.rb:68:in `initialize'
app/models/user.rb:68:in `open'
app/models/user.rb:68:in `parse_headers'
app/models/user.rb:62:in `import'
app/controllers/users_controller.rb:82:in `import'
Rendering /Users/developer/.rvm/gems/ruby-2.3.1/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.text.erb
Rendering /Users/developer/.rvm/gems/ruby-2.3.1/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_source.text.erb
Rendered /Users/developer/.rvm/gems/ruby-2.3.1/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_source.text.erb (0.5ms)
Rendering /Users/developer/.rvm/gems/ruby-2.3.1/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_trace.text.erb
Rendered /Users/developer/.rvm/gems/ruby-2.3.1/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_trace.text.erb (0.5ms)
Rendering /Users/developer/.rvm/gems/ruby-2.3.1/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.text.erb
Rendered /Users/developer/.rvm/gems/ruby-2.3.1/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.text.erb (0.7ms)
Rendered /Users/developer/.rvm/gems/ruby-2.3.1/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.text.erb (45.3ms)
<li><%= link_to "Click Here", import_users_path, :method => :post, remote: true, :id => 'user-import', "data-toggle" => "modal", 'data-target' => '#userImport' %></li>