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) ...
Related
The command
mix phx.gen.html Blog Post posts title body:text
generates among other files the template form.html.eex for the add/edit posts form, which looks like this:
<%= form_for #changeset, #action, fn f -> %>
<%= if #changeset.action do %>
<div class="alert alert-danger">
<p>Oops, something went wrong! Please check the errors below.</p>
</div>
<% end %>
<div class="form-group">
<%= label f, :title, class: "control-label" %>
<%= text_input f, :title, class: "form-control" %>
<%= error_tag f, :title %>
</div>
<div class="form-group">
<%= label f, :body, class: "control-label" %>
<%= textarea f, :body, class: "form-control" %>
<%= error_tag f, :body %>
</div>
<div class="form-group">
<%= submit "Submit", class: "btn btn-primary" %>
</div>
<% end %>
The form_for function gets #action argument which is not present in the PostController, so where does it come from? I couldn't find.
In my installation I have a plug MyProject.Locale which redirects all unlocalized requests to localized ones like:
/posts --> /de/posts
And I have the :locale scope in my router
scope "/:locale", MyProject.Web, as: :locale do
pipe_through :browser
get "/", PageController, :index
resources "/posts", PostController
end
The default #action doesn't take the scope's locale chunk into account and sends the POST request to the /posts url, which is redirected by my MyProject.Locale plug to /de/posts as GET request (it uses the function Phoenix.Controller.redirect(to:...)). I want the form to send the POST request to the localized path.
So, can we override this #action argument for all resources somewhere in one place or do we have to provide it in each controller in render function call
render(conn, "new.html", changeset: changeset, action: action)
or the only option is to change the form templates for all resources?
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 %>
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.
Hi I have any form with nested form, for example
<% form_for :main do |f| %>
trying to insert code here
<% fields_for :nested do |nested_form| %>
<%= nested_form.text_field :description %>
<% end %>
<% end %>
And then I am trying to insert anything to a main form, nested form doesn't produce any output. It outputs only when it is the only object in main form. Any suggestions?
From the Rails 3 documentation examples you need to write your form_for like this:
<%= form_for :main do |f| %>
# trying to insert code here
<%= fields_for :nested do |nested_form| %>
<%= nested_form.text_field :description %>
<% end %>
<% end %>
Note the <%= for both form_for and fields_for