【问题标题】:ActionView::Template::Error (undefined method `to_model' for true:TrueClass for Update Attribute MethodActionView::Template::Error (未定义方法 `to_model' 为 true:TrueClass 为更新属性方法
【发布时间】:2019-06-14 05:33:03
【问题描述】:

所以这实际上是之前编码问题的延续,可以在 here 找到。上一个问题问后端出了什么问题。但是现在我遇到了前端代码的问题。

我正在尝试创建一个按钮,单击该按钮将更新与收藏的关系,如果已经收藏则不收藏。

partials/_favorite.html.erb

<%= form_tag current_user.favorite(@user), remote: true do |f| %>     # partials/_favorite.html.erb:1
    <div>
        <%= hidden_field_tag :followed_id, @user.id %>
    </div>
    <%= button_tag(class: "d-block mx-auto btn btn-warning") do %>
        <%= icon('far', 'star') %>
    <% end %>
<% end %>

forms/_favorite.html.erb

<% unless current_user?(@user) %>
    <div id="follow_form">
        <% if current_user.favorited?(@user) %>
            <%= render 'partials/unfavorite' %>
        <% else %>
            <%= render 'partials/favorite' %>     # forms/_favorite.html.erb:1
        <% end %>
    </div>
<% end %>

_stats.html.erb

<% @user ||= current_user %>

<div class="row">
    <div class="col-4 text-center">
        <%= link_to following_user_path(@user) do %>
            <b>Following</b><br>
            <%= @user.following.count %>
        <% end %>
    </div>
    <div class="col-4 text-center">
        <%= link_to followers_user_path(@user) do %>
            <b>Followers</b><br>
            <%= @user.followers.count %>
        <% end %>
    </div>
    <div class="col-4">
        <%= render 'forms/follow', followed_id: @user.id %>
        <%= render 'forms/favorite', followed_id: @user.id if current_user.following?(@user) %>     # _stats.html.erb:18
    </div>
</div>

我已经在这个相同的功能接口上工作了大约一个星期,但我一直未能成功实现它。由于上一个问题中的解决方案,最喜欢和最不喜欢的方法可以成功运行。测试套件没有显示任何错误。

我不明白我收到的错误是我无法解决编码问题的主要原因。我将不胜感激。

我遇到的错误是:

ActionView::Template::Error (undefined method `to_model' for true:TrueClass
Did you mean?  to_yaml):
    1: <%= form_tag current_user.favorite(@user), remote: true do |f| %>
    2:  <div>
    3:          <%= hidden_field_tag :followed_id, @user.id %>
    4:  </div>

app/views/partials/_favorite.html.erb:1:in `_app_views_partials__favorite_html_erb__875601414_141134960'
app/views/forms/_favorite.html.erb:6:in `_app_views_forms__favorite_html_erb__719356774_141387060'
app/views/partials/_stats.html.erb:18:in `_app_views_partials__stats_html_erb__832871257_32744880'
app/views/users/show.html.erb:13:in `_app_views_users_show_html_erb__718176203_36043580'

【问题讨论】:

    标签: ruby-on-rails forms actionview update-attributes


    【解决方案1】:

    我终于解决了这个更新关系属性的问题。

    首先,我必须确定我收到的错误是什么意思。对于那些对 Rails 还比较陌生的人,“未定义方法 `to_model' for true:TrueClass”意味着您正在尝试创建一个条件语句没有条件

    语句:current_user.favorite(@user) 和 current_user.unfavorite(@user) 返回 true,程序不知道如何评估。因此我不得不改变:

    partials/_favorite.html.erb

    <%= form_tag current_user.favorite(@user), remote: true do |f| %>
        <div>
            <%= hidden_field_tag :followed_id, @user.id %>
        </div>
        <%= button_tag(class: "d-block mx-auto btn btn-warning") do %>
            <%= icon('far', 'star') %>
        <% end %>
    <% end %>
    

    到...

    <%= link_to favorite_relationship_path(current_user.active_relationships.find_by(followed_id: @user.id)), method: :put, remote: true do %>
        <%= button_tag(class: "d-inline btn btn-warning") do %>
            <%= icon('far', 'star') %>
        <% end %>
    <% end %>
    

    partials/_unfavorite.html.erb

    类似

    这也意味着我需要在关系控制器中引入后续操作

    Relationships_controller.rb

      def favorite
        @user = Relationship.find(params[:id]).followed
        current_user.favorite(@user)
        respond_to do |format|
          # Handle a Successful Unfollow
          format.html
          format.js
        end
      end
    
      def unfavorite
        @user = Relationship.find(params[:id]).followed
        current_user.unfavorite(@user)
        respond_to do |format|
          # Handle a Successful Unfollow
          format.html
          format.js
        end
      end
    

    然后这些控制器操作将循环回我的用户模型方法:

    用户.rb

    def favorite(other_user)
        active_relationships.find_by(followed_id: other_user.id).favorite
    end
    
    def unfavorite(other_user)
        active_relationships.find_by(followed_id: other_user.id).unfavorite
    end
    

    这又会触发关系模型成功更新属性:

    Relationship.rb

    def favorite
        update_attribute(:favorited, true)
    end
    
    def unfavorite
        update_attribute(:favorited, false)
    end
    

    【讨论】:

      猜你喜欢
      • 2015-06-11
      • 1970-01-01
      • 1970-01-01
      • 2014-09-17
      • 2013-04-10
      • 2015-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多