开发者

rails update and create using form template

开发者 https://www.devze.com 2023-02-05 06:49 出处:网络
I would like to know where i am wrong. So i have an form template to edit and update model(more than one)

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


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!):

  • ActionController Overview
  • Rails Routing from the Outside In


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, …
0

精彩评论

暂无评论...
验证码 换一张
取 消