Javascript 轨道3链接到部分,内部带有for环
我以前从未编程过,现在很难在Rails 3应用程序的用户配置文件中获得Javascript 轨道3链接到部分,内部带有for环,javascript,ruby-on-rails-3,partial-views,Javascript,Ruby On Rails 3,Partial Views,我以前从未编程过,现在很难在Rails 3应用程序的用户配置文件中获得链接到,以呈现相应的:部分。总而言之,有三个部分: _profile\u credits(为了这个问题,我将重点关注\u profile\u credits) _简介 _个人资料审查 我要做的是单击以下内容: <li><%= link_to "Credits", profile_credits_profile_path(:id => @profile.id), :remote => true %&
链接到
,以呈现相应的:部分
。总而言之,有三个部分:
\u profile\u credits
)<li><%= link_to "Credits", profile_credits_profile_path(:id => @profile.id), :remote => true %></li>
在myroutes.rb中:
resources :profiles do
get :profile_credits, :on => :member
end
$( "#tabs" ).html( "<%= escape_javascript( render (:partial => "profile_credits", :locals => { :id => @profile.id } ) ) %>" );
我的个人资料\u credits.js.erb
:
resources :profiles do
get :profile_credits, :on => :member
end
$( "#tabs" ).html( "<%= escape_javascript( render (:partial => "profile_credits", :locals => { :id => @profile.id } ) ) %>" );
$(“#tabs”).html(“#profile_credits”,:locals=>{:id=>@profile.id}))%>”;
当我点击一个链接时,现在什么也没有发生。我尝试了以下各种Rails3UJS示例,但没有得到它。有一次,我在ProfilesControllerprofile\u credits
操作中指定了更多内容。但是,如果我在其他人的个人资料上,则加载的是我的信用,而不是我正在浏览其个人资料的用户的信用。有人能帮我弄明白吗?我终于把这个弄明白了。给定一个包含
导航和加载内容的
,以下是我的代码:
我的容器的HTML:
<div id="tabs">
<ul id="infoContainer">
<li><%= link_to "Reviews", profile_reviews_profile_path, :remote => true %></li>
<li><%= link_to "About", profile_about_profile_path, :remote => true %></li>
<li><%= link_to "Credits", profile_credits_profile_path, :remote => true %></li>
</ul>
<div id="tabs-1">
<%= render :partial 'profile_reviews %> #default
</div>
</div><!-- end tabs -->
<% for credit in @user.credits %>
<div class="credit">
</div>
<% end %>
$( "#tabs-1" ).html( "<%= escape_javascript( render (:partial => "profile_credits" ) ) %>" );
def profile_credits
@profile = Profile.find(params[:id])
@user = User.find(@profile.user_id)
@credits = User.find(@profile.id).credits
respond_to do |format|
format.html { render :layout => nil }
format.xml
format.js { render :layout => nil }
end
end
profile\u credits.js.erb
:
<div id="tabs">
<ul id="infoContainer">
<li><%= link_to "Reviews", profile_reviews_profile_path, :remote => true %></li>
<li><%= link_to "About", profile_about_profile_path, :remote => true %></li>
<li><%= link_to "Credits", profile_credits_profile_path, :remote => true %></li>
</ul>
<div id="tabs-1">
<%= render :partial 'profile_reviews %> #default
</div>
</div><!-- end tabs -->
<% for credit in @user.credits %>
<div class="credit">
</div>
<% end %>
$( "#tabs-1" ).html( "<%= escape_javascript( render (:partial => "profile_credits" ) ) %>" );
def profile_credits
@profile = Profile.find(params[:id])
@user = User.find(@profile.user_id)
@credits = User.find(@profile.id).credits
respond_to do |format|
format.html { render :layout => nil }
format.xml
format.js { render :layout => nil }
end
end
成功了。既然你说你以前从未编程过,我就提出来。不要将任意变量名用于任何其他非常简单的用途(如i
作为迭代器)。这个问题没有得到回答的部分原因可能是,如果给定名为a
的动作和名为c
的部分动作,任何人都很难猜出发生了什么。这些是什么?明白了,它们是信用卡。我这样命名是为了让我的URL更短,但可能不应该让它决定我如何命名控制器操作和部分。我已经把完整的操作名称放在里面供您参考。谢谢你的提示。你的个人资料是指@user
,你正在将@profile.id
作为本地用户传递给它。那么@user
和@profile
之间的关系是什么呢?您是否有一个用户模型,或者@User
只是profile\u控制器中的一个实例变量?是什么将@user
和@profile
联系在一起的?也许你想在你的个人资料中引用@profile
?我有用户
和个人资料
的模型。用户有一个:配置文件。配置文件属于:用户
。