Ruby on Rails go to a specific page when clicking "f.submit" - ruby-on-rails

In my Ruby on Rails application I have the following form for booking_line view:
<%= form_for #booking_line do |f| %>
<%= f.hidden_field :showing_id %>
<%= image_tag "thor_hammer.jpg",:size => "900x250" %>
<td width="300px"> <br><%= f.label :seat_id, 'Please Select a Seat:' %>
<br><%= f.collection_select :seat_id, free_seats, :id,:seats_available %><br>
</td>
<td width="300px">
<div class="actions">
<%= f.submit %>
</div>
<br>
<%= render "/error_messages", :message_header => "Cannot save: ", :target => #booking_line %>
</td>
<% end %>
But as this is booking_line, what I want to happen is that when the submit button is clicked it creates a booking and a booking_line because in the booking_line table it has the attribute booking_id so that the association is made between the two. How can I achieve this?

you can use url in form_for method..to overwrite individual conventions, such as:
<%= form_for(#post, url: my_custom_path) do |f| %>
...
<% end %>
and then add your own logic at my_custom action to achieve it

Related

Passing a value from one view to another

I have a button on an index page that links to a new_assignment_path
<% #users.each do |user| %>
<tr>
<td><%= link_to user.name, user %></td>
<td><%= link_to 'Assign to Class', new_assignment_path, :class => 'btn btn-mini' %></td>
</tr>
<% end %>
And I want it so that when you click on it, it takes you to the new_assignment_path, and takes the dropdown select on that pages form.
<%= simple_form_for(#assignment) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :user_id, collection: User.all.collect, as: :select %>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
And have the drop down automatically set to the user.id of whatever user the button was inside of.
What is the best way of doing this?
Add params[:user_id] in the selected option of your select tag
<div class="form-inputs">
<%= f.input :user_id, collection: User.all.collect, as: :select, :selected => params[:user_id] %>
</div>

Select tag rails

I have a view where I need to show a list of pages (I have a model called Page) and then i need the id of the selected page
I have this code in the view
مهمة جديدة
: <%= form_for :task, :url => {:action=>"create", :controller=>"tasks"}, :html => {:class :
=> "nifty_form"} do |f| %>
<% if #task.errors.any? %>
<div id="error_explanation">
<h2>: عذرا لم نستطع استكمال طلبك</h2>
<ul>
<% #task.errors.full_messages.each do |msg| %>
<li style="color:red;"><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label "اسم المهمة" %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label "وصف المهمة" %><br />
<%= f.text_area :description %>
</div>
<div class="actions">
<button><%= link_to 'العودة الى قائمة المهام', project_tasks_path %></button>
<%= f.submit "مهمة جديدة"%>
</div>
<%= select_tag(:page, options_for_select(['a',1])) %>
<% end %>
#pageslist is a 2d array of the form [['pagename', 1], ..]
The question is how can i access the page_id of the selected page in the controller ?
I tried params[:p_id] but it is not working any help please?
This is what I have in the controller:
#task = Project.find(params[:project_id]).tasks.new(params[:task])
#task.update_attributes({:page_id => params[:p_id]})
Assuming that #pagelist is in a format as you described, then in your view:
<%= select_tag(:page, options_for_select( #pagelist )) %>
You had missed it in your example, and provided only 1d array for options_for_select.
With such prepared view your create action will receive page id in:
params[:page]
it's because you had passed :page as a first argument for select_tag.

blank comment entry with rails

I'm trying to add ability to add comments to projects within a "todo" type app and have run into a problem.
I've created a project with comments before and never run into this problem, but basically rails is drawing an empty comment on the project page.
I've tried a few if statements without any luck, does anyone see my problem ?
<% #project.comments.each do |comment| %>
<div class="commentBlock"><strong><%= comment.posted_by %> says:</strong>
<%=raw comment.comment %>
<small><i class="icon-remove"></i> <%= link_to 'Delete', [comment.project, comment],:confirm => 'Are you sure?',:method => :delete %></small></div>
<% end %>
<h3>Leave a comment</h3>
<%= form_for([#project, #project.comments.build]) do |f| %>
<div class="field">
<%= f.hidden_field :posted_by, :value => current_user.username %>
</div>
<div class="field">
<%= f.label :comment %><br />
<%= f.text_area :comment, :class => "tinymce" %><%= tinymce %>
</div>
<p><%= f.submit :class => 'btn' %></p>
<% end %>
Answer was an error in my controller for Projects, referenced #comment like so:
#comment = #project.comments.build(params[:comment]) by accident!!
Changed to:
#comment = #project.comments
And all works as it should :P
thanks for your help, bad "end of the day" error right there :P

Using nested forms in Rails - How can I make the show page display every new entry in a table format depending on how many presents the user wants?

I have a baby present registry I'm building with Rails 3.2. There is a registry model and a present model nested under the registry model. Everything works 100% except in the show page.
When a user creates a registry he/she can add presents through the add presents tag (as many as he/she wants), but the problem is in the show page it only the first present is displayed as I want it too. The rest is displayed in plain non formatted text.
Any help please? I would like it to show in a table format.
Here is my code:
Here is the Registry Form:
<%= nested_form_for(#registry) do |f| %>
<% if #registry.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#registry.errors.count, "error") %> prohibited this registry from
being saved:</h2>
<ul>
<% #registry.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.label :due_date %><br />
<%= f.date_select :due_date, :rows => 5 %>
<%= f.label :theme %><br />
<%= f.text_field :theme, :class => 'field2' %>
<div class="field1">
<%= f.label :gender %><br />
<%= f.text_field :gender %>
</div>
<div class="field1">
<%= f.fields_for :presents do |builder| %>
<%= render 'present_fields', f: builder %>
<% end %>
</div>
<%= link_to_add_fields "Add Presents", f, :presents %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
The presents_fields:
<div class="field1">
<table>
<tr>
<td> <%= f.label :type, "Present Type" %> </td>
<td> <%= f.text_field :type, :class => 'field2' %> </td>
<td> <%= f.label :Quantity, "Quantity" %> </td>
<td> <%= f.number_field :quantity, :class => 'field3' %> </td>
<td> <%= f.label :color, "Color" %> </td>
<td> <%= f.text_field :color, :class => 'field2' %> </td>
<td> <%= f.label :brand, "Brand" %> </td>
<td> <%= f.text_field :brand, :class => 'field2' %> </td>
<td> <%= f.link_to_remove "Remove this present" %></td>
</tr>
</table>
</div>
The show page:
<% provide(:title, 'My Party and Present Whishes') %>
<%= notice %>
<p> Baby & Party Details: </p></br>
<li><p> Due date: </p> <%= #registry.due_date %></li></br>
<li><p> Party Theme: </p><%= #registry.theme %></li></br>
<li><p> Gender: </p><%= #registry.gender %></li></br>
<p> Presents Registry: </p></br>
<li>
<% #registry.presents.each do |registry| %>
<%= registry.type %>
<%= registry.quantity %>
<%= registry.color %>
<%= registry.brand %>
</li>
<% end %>
</br>
</br>
<%= link_to 'Edit', edit_registry_path(#registry) %>
<%= link_to 'Back', registries_path %>
<li>
<% #registry.presents.each do |registry| %>
Should not these lines be inverted?
<% #registry.presents.each do |registry| %>
<li>

Rails: radio button selection for nested forms objects

I have the following form for photo_album which uses the nested forms feature of the Rails in saving the photos while updating the photo_album. And having trouble with the selection of radio button value. I want only one of the photos be able to select as the album cover, but due to the way rails produces form element ids and names, I am able to select all of the photos as album covers. Is there any workaround?
<% form_for #photo_album do |f| %>
<%= f.error_messages %>
<% #photo_album.photos.each do |photo| %>
<% f.fields_for :photos, photo do |photo_fields| %>
<p>
<%= image_tag url_for_image_column(photo, "data", :thumb) %>
</p>
<p>
<%= photo_fields.label :title %>
<%= photo_fields.text_field :title %>
</p>
<p>
<%= photo_fields.label :body %>
<%= photo_fields.text_area :body %>
</p>
<p>
<%= photo_fields.radio_button :cover, "1" %>
<%= photo_fields.label :cover, 'Album Cover', :class => 'option' %>
<%= photo_fields.check_box :_delete %>
<%= photo_fields.label :_delete, 'Delete', :class => 'option' %>
</p>
<% end %>
<% end %>
<p>
<%= f.submit #photo_album.new_record? ? 'Create' : 'Update' %>
</p>
<% end %>
And following is the html produced by rails (which is part of the problem) for radio buttons:
<p>
<input type="radio" value="1" name="photo_album[photos_attributes][0][cover]" id="photo_album_photos_attributes_0_cover_1"/>
<label for="photo_album_photos_attributes_0_cover" class="option">Album Cover</label>
<input type="hidden" value="0" name="photo_album[photos_attributes][0][_delete]"/><input type="checkbox" value="1" name="photo_album[photos_attributes][0][_delete]" id="photo_album_photos_attributes_0__delete"/>
<label for="photo_album_photos_attributes_0__delete" class="option">Delete</label>
</p>
I would guess that in this instance you'd want to use radio_button_tag instead and use the same name.
This should be close - you will still need to figure out when you want to pass in true:
<%= radio_button_tag "cover", photo.id, todo %>
You might also be able to specify the name with the radio_button helper, but it doesn't quite fit the model so radio_button_tag makes more sense to me - I could be mistaken.

Resources