Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如果第一个值没有';不存在,比如ruby/rails中的某种类型的回退?_Ruby On Rails_Ruby_Ruby On Rails 3_Ruby On Rails 3.2 - Fatal编程技术网

Ruby on rails 如果第一个值没有';不存在,比如ruby/rails中的某种类型的回退?

Ruby on rails 如果第一个值没有';不存在,比如ruby/rails中的某种类型的回退?,ruby-on-rails,ruby,ruby-on-rails-3,ruby-on-rails-3.2,Ruby On Rails,Ruby,Ruby On Rails 3,Ruby On Rails 3.2,基本上,我有一个follow按钮,当点击页面刷新时,我会显示一个unfollow按钮。下面是我用来呈现所需特定表单的代码: 遵循部分表格: <% unless current_user?(@user) %> <% if current_user.following?(@user) %> <%= render 'relationships/partials/unfollow' %> <% else %> <%= rend

基本上,我有一个follow按钮,当点击页面刷新时,我会显示一个unfollow按钮。下面是我用来呈现所需特定表单的代码:

遵循部分表格:

<% unless current_user?(@user) %>
  <% if current_user.following?(@user) %>
    <%= render 'relationships/partials/unfollow' %>
  <% else %>
    <%= render 'relationships/partials/follow' %>
  <% end %>
<% end %>
class RelationshipsController < ApplicationController

  def get_follow_form
    respond_to do |format|
      format.html { render :partial => 'relationships/partials/follow_form_ajax' }
    end
  end
end
<%= form_for current_user.relationships.build(:followed_id => @user.id), :remote => true do |f| %>
<%= f.hidden_field :followed_id %>
<%= f.submit "Follow", :class => 'followButton' %>
<% end %>

任何时候,我都会将表单更改为ajax表单,因为我不希望页面刷新,并且在表单提交成功后,我希望将follow按钮/表单替换为unfollow按钮/表单。这不是直截了当的,因为一次只显示一个表单,所以我无法使用jquery选择器来查找此表单

我决定做的是创建一个新操作,以使follow_表单部分化,这样我就可以使用jquery选择器操作适当的表单

新动作:

<% unless current_user?(@user) %>
  <% if current_user.following?(@user) %>
    <%= render 'relationships/partials/unfollow' %>
  <% else %>
    <%= render 'relationships/partials/follow' %>
  <% end %>
<% end %>
class RelationshipsController < ApplicationController

  def get_follow_form
    respond_to do |format|
      format.html { render :partial => 'relationships/partials/follow_form_ajax' }
    end
  end
end
<%= form_for current_user.relationships.build(:followed_id => @user.id), :remote => true do |f| %>
<%= f.hidden_field :followed_id %>
<%= f.submit "Follow", :class => 'followButton' %>
<% end %>
类关系控制器relationships/partials/follow_form_ajax'}
结束
结束
结束
现在的问题是我没有访问@user实例变量的权限。这并不重要,因为我可以让刚刚通过jquery成功数据跟踪的用户在新的ajax调用中将其作为数据传递到get_follow_form_url,然后将该信息作为局部变量传递到partial中

我仍然存在@user实例变量不可用的问题。这就引出了我的问题

如果实例变量不是nil/不存在,如何使用另一个值

以下表格:

<% unless current_user?(@user) %>
  <% if current_user.following?(@user) %>
    <%= render 'relationships/partials/unfollow' %>
  <% else %>
    <%= render 'relationships/partials/follow' %>
  <% end %>
<% end %>
class RelationshipsController < ApplicationController

  def get_follow_form
    respond_to do |format|
      format.html { render :partial => 'relationships/partials/follow_form_ajax' }
    end
  end
end
<%= form_for current_user.relationships.build(:followed_id => @user.id), :remote => true do |f| %>
<%= f.hidden_field :followed_id %>
<%= f.submit "Follow", :class => 'followButton' %>
<% end %>
@user.id),:remote=>true do | f |%>
'跟随按钮'%>
我可以这样做吗

 :followed_id => @user.id <-if this doesn't exist use this-> user.id 
:后跟_id=>@user.id使用类似于或的内容,然后尝试使用逻辑表达式:

:followed_id => @user.andand.id || user.id 
即使没有这些,您也可以使用相同的逻辑,当然不需要多个部分:

:followed_id => (@user && @user.id) || user.id

但正如弗雷德里克所说,如果你已经有了对象的替换值,你就不能设置它吗?

有一个非常简单的方法,假设你有你的“后备”ID:

:followed_id => @user.present? ? @user.id : fallback_id

如果您知道用户是谁,为什么不设置@user?这就是解决方案。我学习代码不够努力。这正盯着我的脸。谢谢不是解决方案,但这是我问题的答案谢谢。是的,我设置了实例变量。弗雷德里克的建议就是解决办法。