Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)

like image 728
Joel Friedlaender Avatar asked Dec 08 '10 11:12

Joel Friedlaender


2 Answers

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 %>
like image 186
nanda Avatar answered Oct 16 '22 08:10

nanda


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

like image 33
Srushti Avatar answered Oct 16 '22 07:10

Srushti