我正在尝试执行一个计算,它看起来像我到目前为止一直在视图中.
在我看来
<% for post in @user.posts %>
<%= post.tasks.count / @projects.tasks.count %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
我正在尝试显示一个百分比....当我自己放置<%= post.tasks.count%>时它显示2和<%= @ projects.tasks.count%>它显示4.
但是当我尝试<%= post.tasks.count/@ projects.tasks.count%>时,它显示0不是.50或1/2.
我在视图而不是控制器中执行此逻辑的原因是我想显示for循环的每次迭代的动态百分比
我试图弄清楚这个HABTM关系问题是为了我的数据库中的用户的利益存储.兴趣表包含具有id和名称的不同兴趣列表(即:id = 1,name ='Music')
我有一个用户模型=> user.rb
has_and_belongs_to_many :interests
Run Code Online (Sandbox Code Playgroud)
和兴趣模型=> interest.rb
has_and_belongs_to_many :users
Run Code Online (Sandbox Code Playgroud)
现在我正在尝试从复选框列表中编辑或更新用户的兴趣选择.控制器看起来像=>
def edit
#@interests = Interest.find(:all)
@title = "Edit Interest"
@user = User.find(session[:user_id])
@user.interest ||= Interest.new
interest = @user.interest
if param_posted?(:interest)
if @user.interest.update_attributes(params[:interest])
flash[:notice] = "Changes saved."
redirect_to :controller => "users", :action => "index"
end
end
end
Run Code Online (Sandbox Code Playgroud)
而param_posted函数看起来像这样
def param_posted?(sym)
request.post? and params[sym]
end
Run Code Online (Sandbox Code Playgroud)
视图逻辑如下所示:
<% for interest in @interest %>
<div>
<%= check_box_tag "user[interest_id][]", interest.id, @user.interests.include (interest) %>
<%= interest.name %>
</div>
<% end %>
Run Code Online (Sandbox Code Playgroud)
我认为一切看起来都很犹豫但是当我运行视图时我得到了错误:
InterestController#edit中的NoMethodError …
checkbox ruby-on-rails relational-database has-and-belongs-to-many
我有
@user = User.find(1)
@event = Event.find(1)
for interest in @event.interests
@user.choices.create(:interest => interest, :score => 1)
end
Run Code Online (Sandbox Code Playgroud)
工作正常,但每次都会创建记录.我想检查是否存在具有来自for循环的相应interest_id的记录,如果它存在,我想更新,否则创建就好了.
我环顾四周,找到了"find_or_create_by"方法,但它并不适合我.我有=>
@user.choices.find_or_create_by_interest(:interest => interest.id, :score => 1)
Run Code Online (Sandbox Code Playgroud)
并得到错误"未定义的方法`find_by_interest'".
你能看出我做错了什么吗?或者坚持使用if/else语句会更好吗?
我不确定find_or_create_by方法是否具有更新属性功能.
我试图弄清楚如何为循环的每次迭代向现有对象添加记录.我很难发现对象和数组之间的区别.
我有这个
@events = Event.find(1)
@loops = Choices.find(:all, :limit => 5) #so loop for 5 instances of choice model
for loop in @loops
@events = Event.find(:all,:conditions => ["event.id = ?", loop.event_id ])
end
Run Code Online (Sandbox Code Playgroud)
我正在尝试根据循环变量的id添加新事件到现有的@events对象.但是(=)运算符只是创建了一个@events对象的新实例.
我尝试(+ =)和(<<)作为运算符,但得到了错误
"当你没想到它时,你有一个零对象!你可能已经预料到了一个Array的实例.在评估nil时出错了"
我试过创建了一个数组
events = []
events << Event.find(1)
@loops = Choices.find(:all, :limit => 5) #so loop for 5 instances of choice model
for loop in @loops
events << Event.find(:all,:conditions => ["event.id = ?", loop.event_id ])
end
Run Code Online (Sandbox Code Playgroud)
但我不知道如何在视图中调用该数组属性
使用对象,我能够在视图中创建一个循环,并调用该对象的所有属性......
<table>
<% for event in @events …Run Code Online (Sandbox Code Playgroud) 我有一个名为Events的Rails模型,其字段/属性称为:description.
我在类型下迁移它,t.text而不是t.string因为我要显示大量数据.
所以......现在我想要以<%= @event.description %>一种简洁的方式展示并想要分解一些句子而不是一大块信息.
我希望我可以插入<p>或其他HTML代码来帮助显示文本.
问题是<p>只在字段中插入打印<p>和html命令的不需要的操作.
如何在文本属性中添加html样式?
我正致力于在rails中创建游戏,我遇到了创建评分逻辑的问题.
我有一个名为Score的模型,它属于一个用户,并有total_points作为属性.
因此,每次用户创建帖子(或其他)时,我都会自动调整用户的total_score属性.
我有一种感觉,我可以在得分模型中的某处创建一个方法,但之前没有这样做,所以我有点困惑.
我有一个问题,我认为我需要在同一个动作中使用重定向和渲染,我不断得到双重渲染错误,所以我想知道是否有人可以看到另一种方式.
我有一个带有选择框和按钮的表单,允许用户从一组城市中进行选择以进行导航.
<%= form_tag root_path, :method => :post do %>
<%= select_tag :city, options_for_select(%w{ Pittsburgh Philadelphia Austin }) %>
<%= submit_tag "Change City" %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
在控制器中,我检查城市参数然后重定向到所需的页面.
def index
if params[:city] == "Pittsburgh"
redirect_to pittsburgh_path
elsif params[:city] == "Philadelphia"
redirect_to philadelphia_path
elsif params[:city] == "Austin"
redirect_to austin_path
end
render :layout => false
end
Run Code Online (Sandbox Code Playgroud)
但是在这个页面上,我创建了一个特定的布局/设计,只在该页面上,所以我只关闭了应用程序布局.
我可以同时执行导航操作并关闭应用程序布局的渲染吗?