このコードはすべて、Michael Hartl による Ruby on Rails チュートリアルに基づいています。with でassigned
アクションを作成しましたusers_controller
def assign
puts "in assign..."
@scout = User.find(params[:follower_id])
@der = User.find(params[:followed_id])
end
私はこれが現在何もしないことを理解していますが、_follow
部分的には、私は持っています
<%= form_for @user, url: { action: "assign" } do |f| %>
<div><%= hidden_field_tag :follower_id, @user.id %></div>
<%= f.label :followed_id, "Assign #{@user.name} to" %>
<%= f.collection_select :following, @ders, :id, :name, prompt: true %>
<%= f.submit "Assign", class: "btn btn-primary" %>
<% end %>
しかし、エラーが発生していますNo route matches {:action=>"assign", :controller=>"users", :id=>"4"}
。私はレールに慣れていないので、これはばかげた質問かもしれません。私は何を間違っていますか?ファイルを変更する必要がありroutes.rb
ますか? また、私が試した場合<%= form_for @user do |f| %>
、コントローラーはどのアクションを使用するかをどのように認識しますか? フォームを表示するアクションに基づいていますか?
編集:私のroutes.rb
ファイルは
Rails.application.routes.draw do
root 'static_pages#home'
get 'help' => 'static_pages#help'
get 'about' => 'static_pages#about'
get 'contact' => 'static_pages#contact'
get 'signup' => 'users#new'
get 'login' => 'sessions#new'
post 'login' => 'sessions#create'
delete 'logout' => 'sessions#destroy'
resources :users do
member do
get :following, :followers
end
end
resources :account_activations, only: [:edit]
resources :password_resets, only: [:new, :create, :edit, :update]
resources :relationships, only: [:create, :destroy]
end
編集 2: @Rich のform_for
ブロックによって生成された HTML は
<form class="edit_user" id="edit_user_5" action="/users/5/assign" accept-charset="UTF-8" method="post">
<input name="utf8" type="hidden" value="✓">
<input type="hidden" name="_method" value="patch">
<input type="hidden" name="authenticity_token" value="...">
<label for="user_followed_id">Assign Mr. Marley Muller to</label>
<select name="user[following]" id="user_following">
<option value="1">Example User</option>
<option value="2">Matthew Swartz</option>
<option value="3">Joseph Swartz</option>
</select>
<input type="submit" name="commit" value="Assign" class="btn btn-primary">
</form>
実際のIDを送信していないため、現在idが見つからないというエラーが発生している理由は理にかなっています(edit_user_5
)
編集 3: リクエストに渡されるパラメーターは次のとおりです。
Started PATCH "/users/6/assign" for 68.100.59.128 at 2015-12-15 03:54:39 +0000
Processing by UsersController#assign as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"...", "user"=>{"following"=>"2"}, "commit"=>"Assign", "id"=>"6"}
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 6]]
Unpermitted parameter: following
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", nil]]
Completed 404 Not Found in 9ms (ActiveRecord: 1.0ms)
ActiveRecord::RecordNotFound (Couldn't find User with 'id'=):
app/controllers/users_controller.rb:69:in `assign'
編集 4: 次のルート ファイルを使用すると、
Rails.application.routes.draw do
root 'static_pages#home'
get 'help' => 'static_pages#help'
get 'about' => 'static_pages#about'
get 'contact' => 'static_pages#contact'
get 'signup' => 'users#new'
get 'login' => 'sessions#new'
post 'login' => 'sessions#create'
delete 'logout' => 'sessions#destroy'
resources :users do
member do
get :following, :followers
match :assign, to: :assign, via: [:post, :patch]
end
end
resources :account_activations, only: [:edit]
resources :password_resets, only: [:new, :create, :edit, :update]
resources :relationships, only: [:create, :destroy]
end
あるユーザーを別のユーザーをフォローするように割り当てるための次のフォーム、
<%= form_for @user, url: { action: "assign" } do |f| %>
<%= f.label :follower_id, "Assign #{@user.name} to" %>
<%= f.collection_select :following, @ders, :id, :name, prompt: true %>
<%= f.submit "Assign", class: "btn btn-primary" %>
<% end %>
のActiveRecord::RecordNotFound in UsersController#assign
エラーが発生Couldn't find User with 'id'=
していますが、次のパラメーターがあります。
{"utf8"=>"✓",
"_method"=>"patch",
"authenticity_token"=>"...",
"user"=>{"following"=>"3"},
"commit"=>"Assign",
"id"=>"4"}
ID は正しいです:"user"=>{"following"=>"3"}
と"id"=>"4"
、間違ってアクセスしようとしているだけだと思います。これは users コントローラの assigns アクションです:
def assign
@scout = User.find(params[:id])
@der = User.find(params_hash[:followed_id])
# make scout follow der here
redirect_to @scout
end
考え?