Rails 5.1强参数已被弃用?

时间:2018-01-01 20:30:23

标签: ruby-on-rails ruby ruby-on-rails-5.1

我正在使用Rails 5.1.3中的override func unwind(for unwindSegue: UIStoryboardSegue, towardsViewController subsequentVC: UIViewController) { } & Ruby 2.3.0

我找到的最好的文档就在这里

第533行

Rails Github Repo,但我仍然不清楚。

form_with

代码:

# The parameters in the forms are accessible in controllers according to
# their name nesting. So inputs named +title+ and <tt>post[title]</tt> are
# accessible as <tt>params[:title]</tt> and <tt>params[:post][:title]</tt>
# respectively.

我知道命名有点令人困惑,我仍然没有弄清楚如何让# friendships_controller.rb ... private def friendship_params params.require(:friendship).permit(:user_id, :id) end # Works def destroy Friendship.remove_friend(current_user.id, params[:id].to_i) end # Doesn't work def destroy Friendship.remove_friend(friendship_params[:user_id].to_i, friendship_params[:id].to_i) end # form_with <%= form_with model: Friendship, url: user_friendship_path( user_id: current_user.id id: other_user.id, ), method: :delete do |f| %> <button class='button'></button> <% end %> 正确映射到我需要的路线。 位于路径的form_with friendships#destroy

/users/:user_id/friendships/:id(.:format)

错误消息

resources :users do
  resources :friendships
end

也许我不应该说它不起作用,无论有没有Started DELETE "/users/1/friendships/8" for 127.0.0.1 at 2018-01-02 03:21:40 +0700 Processing by FriendshipsController#destroy as JS Parameters: {"utf8"=>"✓", "authenticity_token"=>"fWnYwpNDItctgg3q/TYIXy5VRO55nwQRINCCykMsDPAFBfwJKjMZ1dneNbg 5yFNHaQP+lXR4ViTje6mK+dCmVg==", "user_id"=>"1", "id"=>"8"} User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]] (0.2ms) BEGIN (0.1ms) COMMIT Friendship Load (1.0ms) SELECT "friendships".* FROM "friendships" WHERE "friendships"."friend_id" = $1 AND "friendships"."user_id" = $2 ORDER BY "friendships"."id" ASC LIMIT $3 [["friend_id", 1], ["user_id", 8], ["LIMIT", 1]] SQL (1.2ms) DELETE FROM "friendships" WHERE "friendships"."id" = $1 [["id", 499]] Friendship Load (0.5ms) SELECT "friendships".* FROM "friendships" WHERE "friendships"."friend_id" = $1 AND "friendships"."user_id" = $2 ORDER BY "friendships"."id" ASC LIMIT $3 [["friend_id", 8], ["user_id", 1], ["LIMIT", 1]] SQL (0.9ms) DELETE FROM "friendships" WHERE "friendships"."id" = $1 [["id", 498]] Completed 400 Bad Request in 13ms (ActiveRecord: 4.2ms) ActionController::ParameterMissing (param is missing or the value is empty: friendship): app/controllers/friendships_controller.rb:40:in `friendship_params' app/controllers/friendships_controller.rb:22:in `destroy' ,行为都是我的期望。但是,返回friendship_params的服务器日志对我来说是一个红旗。

提前感谢任何建议〜!

3 个答案:

答案 0 :(得分:1)

路由是嵌套资源,因此您应该为路径提供两个参数。你还需要提供范围,以便在params中给它“友谊”键。

<%= form_with scope: :friendship, url: user_friendship_path(current_user, other_user), method: :delete do |f| %>

  <button class='button'></button>

<% end %>

答案 1 :(得分:1)

这段代码有点令人困惑,我建议简单的方法就像改变你的路线一样

resources :users do
  resources :friendships
end

更改为

resources :users
resources :friendships

并在视图上

<%= link_to "Remove Friend", friendship_path(friend_id), method: :delete, data: {confirm: "Are you sure"}, class: "btn btn-xs btn-danger" %>

最后是控制器

def destroy
    @friendship = current_user.friendships.where(friend_id: params[:id]).first
    @friendship.destroy
    flash[:notice] = "Friend was removed"
    redirect_to(my_friends_path)
end

希望能提供帮助

答案 2 :(得分:0)

答案是form_with的参数不会将实际参数传递给 Friendship,我们必须在体内手动传递它们形式。

<%= form_with model: Friendship, url: 
  user_friendship_path(
    current_user, other_user
  ), method: :delete do |f| %>

  <%= f.hidden_field :id, value: other_user.id %>

  <button class='button'></button>

<% end %>