开发者

Partial form in rails 3 needing different link depending on New or Edit

I have a basic setup of views that was generated by the Rails 3 scaffolding. It gives me a partial view _form.html.erb. Both my edit.html.erb and my new.html.erb render this partial view.

In the partial view, I want to have a link_to that goes to different paths depending on if it is being rendered by the new or the edit view.

Is there an easy way to do this?

My code looks like this currently, but does not allow for different paths.

<%= f.submit %> or <%= link_to 'Go back', models_path %>

If it helps, I am trying to send them back to the page they came from (they come开发者_如何转开发 from different places for add and edit)


You can use form.object.new_record? instead of params[:action] to know if you are editing or creating (edit view versus new view).

eg:

<%= simple_form_for(@item) do |f| %>  
    <% if @item.new_record? %>  
       <%= f.input :lost_time, input_html: { value: DateTime.now } %>  
    <% else %>                      
       <%= f.input :lost_time, input_html: { value: @item.lost_time } %>    
    <% end %>
<% end %>


I am not that familiar with rails 3. But hope this works for you

<% if params[:action] == 'new' %>
    # code for new
<% elsif params[:action] == 'edit' %>
    # code for edit
<% end %>

GOOD LUCK :D


Have a look at link_to(:back).

That might be what you need here.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜