Rails 使用表单模板更新和创建

发布于 2024-10-12 03:47:04 字数 1287 浏览 4 评论 0原文

我想知道我错在哪里。所以我有一个表单模板来编辑和更新模型(多个)

<%= 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.CarId %>; 但是当我按下“保存”按钮时什么也没有发生,我的意思是 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.CarId %>
But when i press Save button nothings happen I mean create_auto and save_auto are not run

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

把梦留给海 2024-10-19 03:47:04

老实说,您需要退后一步并学习一些 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!):

绅士风度i 2024-10-19 03:47:04

您正在使用非常规方法名称。如果您想这样做,则必须在表单中指定它们。否则我建议重命名这些方法以匹配 Rails 约定。

create, update, …

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.

create, update, …
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文