Rails语法错误 - 无法找到它的原因

时间:2012-09-12 22:06:43

标签: ruby-on-rails-3

我正在尝试实现以下功能,通过按下页面上的按钮,用户可以从数据库中获取一个随机项目,但该项目将具有可编辑的属性。我遇到的问题是,以下我不知道如何传递一个随机id(在数据库中有效),在我的部分中呈现,但仍然能够更新特定属性。

标题中的问题:

  SyntaxError in Tasks#main
  Showing /home/bogdan/ex/bored/app/views/tasks/main.html.erb where line #1 raised:
/home/bogdan/ex/bored/app/views/tasks/main.html.erb:1: syntax error, unexpected ',', expecting ')'
...uffer.append=  form_for (:task, :url => {:action =>'rand_tas...
...                               ^
/home/bogdan/ex/bored/app/views/tasks/main.html.erb:1: syntax error, unexpected ')', expecting keyword_end
...url => {:action =>'rand_task'}) do |f|@output_buffer.safe_co...
...                               ^
../app/views/tasks/main.html.erb:8: syntax error, unexpected keyword_ensure, expecting $end 
Extracted source (around line #1):
1: <%= form_for (:task, :url => {:action =>'rand_task'}) do |f|%>
2:     <%= render(:partial => "rand_show", :locals => {:f => f}) %></p> 
3: <%end%>
4: 
Trace of template inclusion: app/views/tasks/main.html.erb

我试图将rand_task输出对象传递给_rand_show.html.erb,并且afterwartds仅为attr i调用一个新的可编辑表单id

形式:

<%= form_for(@task = Task.new) do |f| %>
  <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name, :disabled=>true %>
  </div>
  <div class="field">
    <%= f.label :category %><br />
    <%= f.text_field :category, :disabled=>true %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

随机方法:

def rand_task
    if params[:id] == 'random'
      @task = Task.order('RANDOM()').first
    else
      @task = Task.order('RANDOM()').first // if not  i get a nill related error.
    end 
  end

请帮忙为什么我会收到上述错误?这是我在想错的概念吗?

1 个答案:

答案 0 :(得分:2)

<%= form_for (:task, :url => {:action =>'rand_task'}) do |f| %>
            ^

问题是form_for(之间的空格。如果你删除它应该工作的空间。