Rails 使用表单模板更新和创建
我想知道我错在哪里。所以我有一个表单模板来编辑和更新模型(多个)
<%= form_for :Car,@car,:url=>{:controller=>:cargo} do |form| %>
....
<%= form.submit "Save", :class => "submit" ,:class =>"Button_style"%>
<% end %>
并且在控制器(货物)中我有一些方法
def index
@cars=Car.find_all_by_UserId(session[:user_id])
if @cars.nil?
end
end
def create_auto
@car = Car.new(params[:Car])
@car.UserId=session[:user_id];
if @car.save
redirect_to :action=>:index
else
render :action => "new_auto"
end
end
def new_auto
@car = Car.new
@car.CarProperty.build
end
def edit_auto
@car = Car.find(params[:id])
if @car.nil?
flash[:notice] = "Empty request"
end
end
def update_auto
@car = Car.find(params[:id])
if @car.update_attributes(params[:Car])
else
render :action => "edit_auto"
end
end
来添加新车我使用按钮
<%= button_to "Add car",{:action=>:new_auto},{:class =>"Button_style",:method => "get"} %>
来编辑 <%= button_to '更改', :controller=>:cargo,:action=>:edit_auto,:id=>car.CarI
d %>; 但是当我按下“保存”按钮时什么也没有发生,我的意思是 create_auto 和 save_auto 没有运行
I would like to know where i am wrong. So i have an form template to edit and update model(more than one)
<%= form_for :Car,@car,:url=>{:controller=>:cargo} do |form| %>
....
<%= form.submit "Save", :class => "submit" ,:class =>"Button_style"%>
<% end %>
And in controller(cargo) i have some method
def index
@cars=Car.find_all_by_UserId(session[:user_id])
if @cars.nil?
end
end
def create_auto
@car = Car.new(params[:Car])
@car.UserId=session[:user_id];
if @car.save
redirect_to :action=>:index
else
render :action => "new_auto"
end
end
def new_auto
@car = Car.new
@car.CarProperty.build
end
def edit_auto
@car = Car.find(params[:id])
if @car.nil?
flash[:notice] = "Empty request"
end
end
def update_auto
@car = Car.find(params[:id])
if @car.update_attributes(params[:Car])
else
render :action => "edit_auto"
end
end
To add new car i use button
<%= button_to "Add car",{:action=>:new_auto},{:class =>"Button_style",:method => "get"} %>
To edit<%= button_to 'Change', :controller=>:cargo,:action=>:edit_auto,:id=>car.CarI
d %>
But when i press Save button nothings happen I mean create_auto and save_auto are not run
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
老实说,您需要退后一步并学习一些 Rails 约定,例如 RESTful 控制器的方法名称。
如果你违背它的约定,Rails 比许多框架更会咬你一口。这通常是一件好事,因为它可以通过不必一直重新发明轮子来提高生产力,但您需要学习它们。
我推荐这些资源(没有双关语!):
Honestly, you need to take a step back and learn some Rails conventions, such as method names for RESTful controllers.
Rails, more than many frameworks, will bite you really hard if you fight its conventions. This is usually a good thing, as it can allow for great productivity by not reinventing the wheel all the time, but you need to learn them.
I recommend these resources (no pun intended!):
您正在使用非常规方法名称。如果您想这样做,则必须在表单中指定它们。否则我建议重命名这些方法以匹配 Rails 约定。
You are using non-conventional method names. If you want to do so, you'll have to specify them in the form. Otherwise I'd suggest to rename the methods to match rails conventions.