1
我正在使用link_to
在我的Rails 3應用程序中創建對象。搜索給了我正確的方法來使用link_to
與:post
方法,但我想知道是否使用link_to
也爲我的對象傳入名稱值。這裏是我的鏈接:使用link_to添加名稱值
<%= link_to "Todo text", {:controller => "profiles", :action => "create_inside", :method => :post}, :class => "button white" %>
我profiles_controller.rb
:
def create_inside
@todo = Insidetodo.new
@todo.save!
if @todo.save
redirect_to @profile.todo, :notice => 'Todo successfully added.'
else
render :action => 'new'
end
end
我todo.rb
型號:
class Todo < ActiveRecord::Base
has_and_belongs_to_many :profiles
validates :name, :presence => true
end
有沒有辦法在:name => "#{@user.profile.todotext}"
添加到link_to
使它通過並保存?我不知道它是否正確創建,因爲當我點擊link_to
時,出現驗證錯誤 - Validation failed: Name can't be blank
。
太好了,謝謝!使用'form_tag'是唯一正確執行該操作的方法嗎?我特別想使用'link_to',因爲它讓我的代碼更清晰。 – tvalent2
實際上你應該使用'button_to'如果你的POST請求 – daniel
難題,有沒有其他的方法來保持名稱的URL? – tvalent2