Rails 4, ruby 2.0.
I want to make a public file serving page with a download link for each file and the possibility to check multiple files for download with checkboxes and a "Download checked files" button.
My code in index.html.erb
<% form_tag(controller: "files", action: "download_many", method: "get")%>
<h1>St.Catherines</h1>
<ul>
<% #stcatherines.each do |file|%>
<li><%= link_to file, :action => "download", :name =>file %></li>
<%check_box_tag(file)%>
<%end%>
<%submit_tag :value => "Download checked files" %>
</ul>
<%end%>
Where #stcatherines is an array of strings
The download link works, something with the form must be wrong.
I'm stuck with a strange error:
.../app/views/files/index.html.erb:11:
syntax error, unexpected keyword_ensure, expecting end-of-input
Note that the syntax error is raised at line 11 and i have only 10 lines of code.
I think that you missed opening the form_tag and added the end, resulting in the syntax error. Adding a do after the form_tag declaration should fix it... Try changing your code to the following (note the addition of do on line 1):
<% form_tag(controller: "files", action: "download_many", method: "get") do %>
<h1>St.Catherines</h1>
<ul>
<% #stcatherines.each do |file|%>
<li><%= link_to file, :action => "download", :name =>file %></li>
<% check_box_tag(file) %>
<% end # each %>
<%submit_tag :value => "Download checked files" %>
</ul>
<% end # form_tag %>
Related
I am attempting to create a form like this:
<%= form_for #form_object, registration_path(#conn, :register), [class: "register_form"], fn f -> %>
<div class="register_alert">
<%= Web.PartialView.render("flash_alert.html", conn: #conn) %>
<%= inputs_for f, :user, [default: %{name_first: nil}], fn fu -> %>
<%= label fu, :name_first, "FIRST NAME", class: "register_label" %>
<%= text_input fu, :name_first, class: "register_text-field"%>
<% end %>
<%= submit "Sign Up", class: "register_register-button" %>
</div>
<%end%>
That #form_object is a the conn passed in at first because I have no changeset until they enter data (create form). However, I keep getting the oh so helpful 'argument error' crash when trying to run this. If I remove the inputs_for section, everything is fine. How do you use inputs_for with a connection object in phoenix?
Phoenix 1.4.0 being used.
Plug.Conn is a struct containing nearly everything about the connection.
Phoenix.HTML.Form.form_for accepts the connection object as a first argument.
Since you were even of less help than Phoenix providing exactly zero information about the error message, I’d suggest the issue is in a well-formedness of #form_object. You clearly have #conn object there on hand, pass it as the first argument:
<%= form_for #conn, registration_path(#conn, :register) ...
I'm trying to simply display 2 fields (first name and last name) in a form select (f.select). Here is my code:
<%= f.select :person, User.where(verified_person: 't').pluck(:first_name, :last_name) %>
With the code above, the select drop-down field only displays first name. I'm using Active Record 4.2 and Rails 4. Any help?
Try Using This Code:
<%=f.select :person, options_for_select(User.where(verified_person: 't').collect {|user| ["#{user.first_name} - #{user.last_name}", user.first_name] }), :include_blank => true%>
I ended up using this.. seems to work for me:
<%= f.select(:person) do %>
<% User.where(verified_person: 't').each do |user| -%>
<%= content_tag(:option, user.first_name + " " + user.last_name, value: user.id) %>
<% end %>
<% end %>
I have a Content model which has one or many Audio files which need to be added by the new/edit form.
What I have did is created the models with this relationship:
class Audio < ActiveRecord::Base
belongs_to :content
has_attached_file :audiofile,
end
class Content < ActiveRecord::Base
...
has_many :audios
accepts_nested_attributes_for :audios, :allow_destroy => true
end
Now in my new Content form I have the following:
<% f.fields_for :audios do |audiof| -%>
<%= f.label :audiofile, 'Audio file:' %>
<%= audiof.file_field :audiofile %>
<% end -%>
What I need it to do is show me the file_field only for a new Audio file and for the existing ones just print me a file size,name and probably a delete button.
I have also created a new record in the controller with:
#content.audios.build
I am using Rails 3.0.3 with Paperclip plugin for upload. Sorry if the question is too nooby.
Thanks.
From my memory, you will be able to access to the instance of the object within the fields_for statement.
Try something like that :
<% f.fields_for :audios do |audiof| -%>
<% if audiof.object.new_record? %>
<%= f.label :audiofile, 'Audio file:' %>
<%= audiof.file_field :audiofile %>
<% else %>
<%= "Filename = #{audiof.object.audiofile.filename}" %>
<%= "url = #{audiof.object.audiofile.url}" %>
<% end %>
<% end -%>
If audiof.object returns nil(In that case, it is not the good name), check by displaying all the public methods <% = raise audiof.public_methods.inspect %>
The object method should return an instance of an Audio class.
I've been stuck on this problem for a couple of days now.
I've have some success with Railscasts Episode #198, but that one is for Rails 2. There have been some changes in Rails 3 that make it so the code provided in Episode #198 won't work.
The problem lies within the edit_individual.html.erb:
Original Code (provided by Ryan # Railscasts):
<% form_tag update_individual_products_path, :method => :put do %>
<% for product in #products %>
<% fields_for "products[]", product do |f| %>
<h2><%=h product.name %></h2>
<%= render "fields", :f => f %>
<% end %>
<% end %>
<p><%= submit_tag "Submit" %></p>
<% end %>
Modified Code (simply changed fields_for to form_for):
<% form_tag update_individual_products_path, :method => :put do %>
<% for product in #products %>
<% form_for "products[]", product do |f| %>
<h2><%=h product.name %></h2>
<%= render "fields", :f => f %>
<% end %>
<% end %>
<p><%= submit_tag "Submit" %></p>
<% end %>
In the new code, each record is placed within a form of their own, all inside one single form (which is the one I only want).
My question is, how can I get the code provided by Railscasts Episode #198 to work in Rails 3?
Here is a link to the Railscast I mentioned:
http://railscasts.com/episodes/198-edit-multiple-individually
Thank You,
c.allen.rosario
I found the solution. Just need to modify the following line in the code provided by Ryan # Railscasts:
<% fields_for "products[]", product do |f| %>
and change it to:
<%= fields_for "products[]", product do |f| %>
Notice, that the <% has been modified to <%=.
final solution:
<% form_tag update_individual_products_path :method => :put do %>
<% for product in #products %>
<%= fields_for "products[]", product do |f| %>
<h2><%= h product.name %></h2>
<% end %>
<% end %>
<p><%= submit_tag "Submit" %></p>
<% end %>
I was wondering if anyone could explain this solution to me. From what I understand you should only need a <% in front of the fields_for.
c.allen.rosario
The change in Rails 3 from <% fields_for to <%= fields_for is because it was confusing that form_for, form_tag, etc... were using <% form... %> even though they they were outputting html code.
With Rails 3, since they output html code, they use <%=.
Please note that your first line is deprecated:
<% form_tag update_individual_products_path, :method => :put do %>
should be
<%= form_tag update_individual_products_path, :method => :put do %>
Same for all form tags.
I have this form:
<% #page_title = "Delete Technician: #{#technician.name}" %>
<%= link_to("<< Back to List", {:action => 'list', :id => #technician.id}, :class => 'back-link') %>
<div class="technician delete">
<h2>Delete Technician</h2>
<%= form_for(:technician, :url => {:action => 'destroy', :id => #technician.id}) do |f| %>
<p>Are you sure you want to permanently delete this technician?</p>
<p class="reference-name"><%= #technician.name %></p>
<div class="form-buttons">
<%= submit_tag("Delete Technician") %>
</div>
<% end %>
</div>
when I click on the submit button this is the url that I get:
www.site.com/technicians/1
instead of
www.site.com/technicians/destroy/1
am I not using the form_for helper correctly or is it a configuration somewhere?
You're making this more complicated than it needs to be. There is no reason for a form when a link or a button would do. Why not just this
<p>Are you sure you want to permanently delete this technician</p>
<div> <%= link_to "Delete", technician_path(#technician), :method => :delete %> </div>
That's it.