I have a code where a user can select a specific file to be deleted or analyzed.
<% if #files%>
<%= form_tag what_to_do_files_path, method: :get do %>
<%= submit_tag "Delete selected", :name => 'delete' %>
<%= submit_tag "Analyse", :name => 'analyse' %>
<% #files.each do |file| %>
<% if (file.analyzed=="no") %>
<p><td> <%= check_box_tag "files[]", file.id %></td><%= file.name %></p>
<% else %>
<div class="my_profile_info">
<p><td> <%= check_box_tag "files[]", file.id %></td> <%= file.name %></p>
<td class="Info">
Info
</td>
</div>
<% end %>
<%end%>
<%end%>
<%else%>
<%end%>
I need to be able to give a name to every analysis.
For example: user selects 3 files, enters a name in the text field "Analysis of annual profit" and clicks on the button "Analyse".
The name "Analysis of the annual profit" and the names of the files that were selected have to be saved into the table group_analysis.
I have tried something like this after submit_tag "Analyse":
<%= form_for #groupanalysis do |f| %>
<div class="field_label">
<%= f.label :group_name, "Type group name hier" %>
</div>
<br class="clear" />
<br />
<% end %>
but it tells me undefined method model name
Thanks in advance.
I think you may need to take a step back and think of how this form represents the model that you're trying to create or update. Generally speaking the first argument to form_for and form_tag is an object and symbol, respectively, which represent the model that you're working with. The form fields map to each attribute of the object.
According to conventions and/or the :url argument, this will get routed to the appropriate controller and call an action according to the HTTP verb (again, part of many conventions in rails).
Going back to your code examples, you are using the form_tag helper incorrectly and the example using form_for may not be the right implementation. For example you're just displaying a label, with no input nor submit.
I hate to just post a link here and just tell you to read the docs, but in this case I think this is the best first step.
http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-form_for
Related
I have two Models with a has many through relationship, and I'm trying to get the form to work properly. I followed the Railscast here for it, and I'm also looking at the API dock on it here.
The code in question is this in my form:
<%= Region.all.each do |region| %>
<%= check_box_tag 'religion[region_ids][]', region.id %>
<%= region.name %><br />
<% end %>
It properly puts out the checkboxes and the names of the regions, but it also prints all the info on each region.
Can anyone tell me why it's doing that?
You have a typo in the first line of your form: <%= Region.all.each do |region| %>. It should not have the equal sign = in the opening erb tag. So change the opening from <%= to <%, as follows:
<% Region.all.each do |region| %>
<%= check_box_tag 'religion[region_ids][]', region.id %>
<%= region.name %><br />
<% end %>
The reason this happens is when you use the equal sign, it will attempt to display the output of the line. And in Ruby the .each method always returns the original array, which is why you are seeing all that extra info.
I came from a JS background so Rails is weird to me. I currently have a show.html.erb for the contest model:
<h1>Contest name: <%= #contest.name %></h1>
<h2>Contest criteria: <%= #contest.criteria %></h2>
<h3>Photos: </h3>
<%= link_to "Enter Contest", "#" %>
<% #contest.photos.each do |photo| %>
<%= image_tag("#{photo}") %>
<% end %>
With the link_to I'm trying to render all photos that belongs to the current_user and pick one of them to assign it to the current contest. The params passing seems all so mysterious to me. Can you guys point me in the right direction of how I should tackle this problem? Thanks
What I understand you need to select an image from list of images and pass the id. So, try this
<%= label_tag "Enter Contest" %>
<% #contest.photos.each do |photo| %>
<%= link_to (image_tag("#{some photo path}")), some_controller_method_path(:photo_id => photo.id) %>
<% end %>
And in controller you can use params[:photo_id]
I have this form:
<tr>
<% item.inventory_items.each do |product| %>
<td>
<%= form_tag("/list_items", method: "post") do %>
<%= hidden_field_tag(:item_id, item.id) %>
<%= hidden_field_tag(:inventory_item_id, product.id) %>
<%= hidden_field_tag(:shopping_list_id, ShoppingList.first.id) %>
<%= submit_tag("#{product.price}", class: "btn btn-primary") %>
<% end %>
</td>
<% end %>
</tr>
Currently the hidden_field for shopping_list_id is being set, as you can see, by ShoppingList.first.id. That was really just a placeholder to make sure my form was working. I want the :user to be able to select which of their lists to submit this list_item to. I'm unsure of the best way to do that. Ideally I'd like to be able to have them hover over the product price and have a drop down of their lists to select from, whereby the form would get the shopping_list_id from. How can I best accomplish something like this? I'm using Twitter Bootstrap. Thanks in advance.
Something like this should get you started:
<%= select_tag(#user, :shopping_list_id, options_for_select(#user.lists)) %>
Ok this is my first app in rails so hopefully this is a simple problem.
Here is my object:
- !ruby/object:ProductImage
attributes:
image_id:
product_id:
created_at:
updated_at:
attributes_cache: {}
This works:
<%= image_form.text_field :product_id %>
But I get undefined method `image_id' for:
<%= image_form.text_field :image_id %>
I just don't get it...
Cheers for any help on this.
This is the actual partial:
<div class="image">
<% new_or_existing = product_image.new_record? ? 'new' : 'existing' %>
<% prefix = "product[#{new_or_existing}_product_image_attributes][]" %>
<% fields_for prefix, product_image do |i| -%>
<div class="input select">
<%= i.text_field :image_id %>
<%= link_to_function "remove", "$(this).up('.image').remove()" %>
</div>
<% end -%>
</div>
P.S a text field is being used just as an example
Can you post the entire form? I have a hunch that maybe you did:
<% form_for :image do |image_form| %>
instead of:
<% form_for :product_image do |product_image_form| %>
Of course, I'd rethink displaying a form where people are manually entering id values in order to join an image to a product, but I understand you're in learning mode right now.
ID fields should not be text_fields, they should be checkboxes or drop-down select boxes. Why would your use know what the Rails ID is?
I've set up a Rails form roughly following the instructions in this Railscast.
Here's the code for the form:
<% form_tag complete_todos_path, :method => :put do %>
<ul>
<div id="incomplete_todos">
<% #incomplete_todos.each do |todo| %>
<%= render :partial => todo %>
<% end %>
</div>
</ul>
<%= submit_tag "Mark as completed" %>
<% end %>
And here's the code for the todo partial:
<div class="todo">
<li>
<%= check_box_tag "todo_ids[]", todo.id %>
<%=h todo.name %>
<%= link_to 'edit', edit_todo_path(todo) %>
<%= link_to 'delete', todo, :confirm => 'Are you sure?', :method => :delete %>
</li>
</div>
It's working great, but I'm looking to start implementing AJAX and I need each checkbox to have a unique id. Right now, the input tags generated look something like this:
<input id="todo_ids_" name="todo_ids[]" type="checkbox" value="7" />
Every check box has the same id ("todo_ids_"), which is a problem. I suspect the solution is embarrassingly simple, but I'm not seeing it. Any tips?
<%= check_box_tag "todo_ids[]", todo.id, false, :id => "todo_id_#{todo.id}" -%> or whatever you want the id to be.
I consider this a bug with check_box_tag caused by the seemingly hackish nature of manually giving it the name todo_ids[] and the method code calling sanitize_to_id(name). I just ran into this yesterday and I'm contemplating a patch.
I ended up using a solution similar to Ryan's, but as I wrote in the comment I had to make a further change. In the form:
<%= check_box_tag "todo_ids[#{todo.id}]", todo.id %>
In the action called by the form:
Todo.update_all(["completed_at = ?", Time.now], :id => params[:todo_ids].keys)
Note the "params[:todo_ids].keys" at the end, which was a workaround to deal with the odd way the parameters were formatted:
"todo_ids" => {"5"=>"5"}
Can you try this and let us know if it works:
check_box_tag "todo_ids[#{todo.id}]", todo.id %>
This is the expected behaviour of check_box_tag, as this comment on a rejected fix explains.
You can use collection_check_boxes like this (haml syntax, sorry):
# Accumulate todos in a params hash like { todos: { to_complete: [] } }
= collection_check_boxes(:todos, :to_complete, #incomplete_todos, :id, :name) do |todo_builder|
= todo_builder.label do
# This is the result of calling :name on the todo, as specified
# calling the helper
= todo_builder.text
= todo_builder.check_box
Of course you can use partials inside the block, just pass and use the builder inside.
Check more options in the API docs.