私はRailsを初めて使用し、Michael HartlのRails 4のチュートリアルhttp://ruby.railstutorial.org/chapters/following-users#sec-following_and_followers_pagesの助けを借りてプロジェクトを行ってい ます.最初の引数を表示しているユーザーをフォローするためのフォームを作成しています. in form に nil を含めたり、Rails 4 で空にしたりすることはできません
<%= form_for(current_user.relationships.find_by(followed_id: @user.id)) do |f| %>
<div>
<%= f.hidden_field :followed_id %>
</div>
<%= f.submit "Follow", class:"btn btn-large" %>
<% end %>
relationship_controller.rb
def create
@user = User.find(params[:relationship][:followed_id])
current_user.follow!(@user)
redirect_to @user
end
このエラーを回避する解決策を教えてください。Railsコンソールを介してリレーションシップテーブルにデータを挿入しています。大丈夫です。しかし、フォームから新しいオブジェクトを作成できず、このエラーが表示されます。
解決 。次のように最初の引数を変更すると、機能しますが、フォロー解除ボタンが表示されません
<%= form_for(Relationship.new(followed_id: @user.id)) do |f| %>