Rails 3 - Can't figure out Create/Update form clause - ruby-on-rails-3

Can someone tell me what I'm doing wrong here? I'm using the same page to do my Create and Update form have this code right at the top of my page:
This works:
<% if #media.blank? %>
<%= form_for(:media, :url => {:action => 'create'}) do |f| %>
<% end %>
This doesn't:
<% if #media.blank? %>
<%= form_for(:media, :url => {:action => 'create'}) do |f| %>
<% else %>
<%= form_for(:media, :url => {:action => 'update', :id => #media.id}) do |f| %>
<% end %>
The latter gives me this result:
syntax error, unexpected keyword_else, expecting keyword_end'); else
Should I be doing my create & update in a different way?
Thanks.

<% if #media.blank? %>
<%= form_for(:media, :url => {:action => 'create'}) do |f| %>
<% end %>
<% else %>
<%= form_for(:media, :url => {:action => 'update', :id => #media.id}) do |f| %>
<% end %>
<% end %>

Related

Couldn't find <model> without an ID, with nested attributes

I have what should be a super simple form which I'm trying to use to update multiple records at once. I'm on Rails 3. I've been through all of the railscasts, etc and am pulling my hair out at this point. I'm using Devise, and have a contacts controller. Users have_many :contacts, and accepts_nested_attributes_for :contacts. The form looks like:
<%= form_for #user, :url => '/updateusercontacts' do |i| %>
<%= i.fields_for :contacts do |f| %>
<%= f.label :first_name %>
<%= f.text_field :firstname %>
<%= f.label :last_name %>
<%= f.text_field :lastname %>
<%= f.label :phone_number %>
<%= f.text_field :phonenumber %>
<%= f.label :user_id %>
<%= f.text_field :id %>
<p>
<% end %>
The form displays properly, but then on submit I get "Can't find Contact without an ID". The controller looks like:
def updatecontacts
#contacts = Contact.find(params[:id])
#contacts.each do |contact|
contact.update_attributes(params[:id])
end
render '/home'
end
The parameters seem correct, and the id's seem to be present and correct, but I can't get the save to work! I'm sure I'm missing something obvious here.
Try to remove the fields_for and when you invoke your find, use this [:user][:contacts]
your code should look like this.
View:
<%= form_for #user, :url => '/updateusercontacts' do |i| %>
<%= i.label :first_name %>
<%= i.text_field :firstname %>
<%= i.label :last_name %>
<%= i.text_field :lastname %>
<%= i.label :phone_number %>
<%= i.text_field :phonenumber %>
<%= i.label :user_id %>
<%= i.text_field :id %>
<p>
<% end %>
Controller
def updatecontacts
#contacts = Contact.find(params[:user][:contacts])
#contacts.each do |contact|
contact.update_attributes(params[:user][:contacts])
end
render '/home'
end
Hope this works.

Rails is not creating hidden field for put method on an update form

I have a REST resource called Greetings.
Here is my routes file:
resources :greetings
I have a form to create or update a greeting as follows:
<%= form_tag(#greeting, :remote => true, :id => 'greeting_form') do %>
<%= text_area :greeting, :content, :rows => 3, :placeholder => "Type your message..." %>
<% end %>
note: I am using form_tag because this form also collects user data.
This is supposed to create a hidden field with method=> put but its not so it can't find the route.
Any ideas how I can get this to submit to the update action?
Just write
<%= form_tag(#greeting, :remote => true, :id => 'greeting_form', :method => :put) do %>
and everything should be working.
You can use form_for tag and still collect user data like this:
<%= form_for #greeting, :validate => true do |f| %>
<%= f.text_area :content, :rows => 3, :placeholder => "Type your message..." %>
<%= f.fields_for #user do |u| %>
<%= u.text_field :name %>
<% end %>
<% end

haml to html.erb

Can anyone please convert these haml code snippets to the equivalent html.erb?
1.
%h1
Edit Project Form
.edit_project
= semantic_form_for [:admin, #project], :url => admin_organization_project_path(#organization), :html => { :multipart => true } do |f|
= f.inputs do
= f.input :name
= f.input :status, :as => :select, :collection => Project.statuses
= f.input :overview
= f.input :funds_purpose
= f.input :goal
.files
= render :partial => 'admin/edit_photo', :collection => #project.project_photos, :locals => { :field_name => 'project[project_photos_attributes][][file]' }
= f.submit 'Save Project'
2.
%li.file.optional#project_project_photo_file_input
= label_tag 'File'
= image_tag edit_photo.file.url(:thumb) if edit_photo.file?
= file_field_tag field_name
For the second:
<li class="file optional" id="project_project_photo_file_input">
<%= label_tag 'File' %>
<%= image_tag edit_photo.file.url(:thumb) if edit_photo.file? %>
<%= file_field_tag field_name %>
</li>
for first:
<h1> Edit Project Form </h1>
<div class='edit_project'>
<%= semantic_form_for [:admin, #project], :url => admin_organization_project_path(#organization), :html => { :multipart => true } do |f| %>
<%= f.inputs do %>
<%= f.input :name %>
<%= f.input :status, :as => :select, :collection => Project.statuses%>
<%= f.input :overview %>
<%= f.input :funds_purpose %>
<%= f.input :goal %>
<div class='files' >
<%= render :partial => 'admin/edit_photo', :collection => #project.project_photos, :locals => { :field_name => 'project[project_photos_attributes][][file]' } %>
</div>
<% end %>
<%= f.submit 'Save Project' %>
<% end %>
</div>

send data to an array from 2 collection select boxes

I have controller with an action step2 which collects all devices by selected category. My step2.html.erb looks like:
<% form_for compare_comparision_path, :url => {:action => 'comparision'} do |f| %>
<%= f.collection_select(:device, #devices, :id, :name, options ={:prompt => "Select"}, :class=>'device') %>
</br>
<%= f.collection_select(:device, #devices, :id, :name, options ={:prompt => "Select"}, :class=>'device') %>
<%= f.submit 'ok' %>
<% end %>
I want for it to allow the user to select two devices and send it to some array or variable in comparison action.
You can do so:
<% form_for compare_comparision_path, :url => {:action => 'comparision'} do |f| %>
<%= f.collection_select('device[]', #devices, :id, :name, options ={:prompt => "Select"}, :class=>'device') %>
</br>
<%= f.collection_select('device[]', #devices, :id, :name, options ={:prompt => "Select"}, :class=>'device') %>
<%= f.submit 'ok' %>
<% end %>
and in controller you`ll have an array in params[:device] containing to selected values.
Or you can replace 'device[]' in my example with unique name for each selectbox (for example 'devise1' and 'device2'.
Then you can get selected values in controller by accessing params[:device1] and params[:device2]

in rails 3.0 I want to use a partial view in a show and in an edit, for edit I want to show the link to delete

<%= question.name %>
Created <%= time_ago_in_words(question.created_at) %> ago.
<% if :action == "edit" then %>
<%= link_to "delete", question, :method => :delete,
:confirm => "You sure?",
:title => question.name %>
<% end %>
Use action_name like so:
<% if action_name == 'edit' %>
<%= link_to 'delete', question, :method => :delete, :confirm => 'You sure?', :title => question.name %>
<% end %>