这种脚手架有什么问题吗?

时间:2023-01-19 16:20:49

Hey guys, i generally haven't used scaffolds, though i've been messing with Rails for quite some time. I would most of the times do things manually.

嘿,伙计们,我一般不会使用脚手架,尽管我已经在Rails上折腾了很长时间了。我大部分时间都是手工做的。

Now, i'm trying to create a restful like form implementation. I have a form_for like :

现在,我想创建一个restful形式实现。我有一个form_for:

<% form_for @alliance, :url => create_alliance_path do |f| %>

   <% if @alliance.errors.any? %>  
   <div id="errorExplanation">  
     <h2><%= pluralize(@alliance.errors.count, "error") %> prohibited this user from being saved:</h2>  
     <ul>  
     <% @alliance.errors.full_messages.each do |msg| %>  
       <li><%= msg %></li>  
     <% end %>  
     </ul>  
   </div>  
   <% end %> 

    <%= f.label :name %>
    <%= f.text_field :name %><br>

    <%= f.label :description %>
    <%= f.text_area :description %><br>

    <%= f.label "Image URL" %>
    <%= f.text_field :image_url %><br>

    <%= f.submit "#{t 'alliance.create_button' }" %>

<% end %>

The routes are :

的路线是:

  scope :path => '/alliances', :controller => :alliances do
    get 'show/(:id)' => :show, :as => 'alliance'
    get 'new' => :new, :as => 'new_alliance'
    post 'create' => :create, :as => 'create_alliance'
  end  

Now, this form is generated by the new action :

现在,这种形式是由新的行动产生的:

def new
    @alliance = Alliance.new
end

And the submission is like :

提交是这样的:

def create
    @alliance = Alliance.new(params[:alliance])

    if @alliance.save
        flash[:error] = I18n.translate 'error.alliance_created'
        redirect_to alliance_path and return
    else
        redirect_to new_alliance_path and return
    end
end 

Now, if there is an error, i do not get it back when redirecting to new_alliance_path. At first sight, this seems normal, as new recreates the @alliance instance variable. However, in some scaffolding code, it seems like it's done in a similar manner.

现在,如果有错误,在重定向到new_alliance_path时,我不会得到它。乍一看,这似乎很正常,因为new重新创建了@alliance实例变量。然而,在一些脚手架代码中,看起来它是用类似的方式完成的。

Can you see what i'm doing incorrectly ?

你能看到我做错了什么吗?

1 个解决方案

#1


1  

Your create action should look more like this:

你的创建行为应该看起来更像这样:

def create
    @alliance = Alliance.new(params[:alliance])

    if @alliance.save
        flash[:error] = I18n.translate 'error.alliance_created'
        redirect_to alliance_path
    else
        render :action => :new
    end
end

When calling redirect you start the complete request life-cycle again. Instead you just want to re-render the new view with the current @alliance object.

调用重定向时,再次启动完整的请求生命周期。相反,您只想用当前的@alliance对象重新呈现新视图。

I also noticed you are using 4 spaces to indent. The Ruby standard is 2.

我还注意到您正在使用4个空格进行缩进。Ruby标准是2。

#1


1  

Your create action should look more like this:

你的创建行为应该看起来更像这样:

def create
    @alliance = Alliance.new(params[:alliance])

    if @alliance.save
        flash[:error] = I18n.translate 'error.alliance_created'
        redirect_to alliance_path
    else
        render :action => :new
    end
end

When calling redirect you start the complete request life-cycle again. Instead you just want to re-render the new view with the current @alliance object.

调用重定向时,再次启动完整的请求生命周期。相反,您只想用当前的@alliance对象重新呈现新视图。

I also noticed you are using 4 spaces to indent. The Ruby standard is 2.

我还注意到您正在使用4个空格进行缩进。Ruby标准是2。