I created a form in Rails 3.2 using the format below, but when I open the post view, I don't see the form to allow me post comments. What could be wrong?
<%= form_tag(:controller => "posts", :action => "create") do %>
<%= label_tag(:message, "What are you doing?") %><br />
<%= text_area_tag(:message, nil, :size => "44x6") %><br />
<%= submit_tag("Update") %>
<% end %>
In Rails 3 the form_* helpers return the markup rather than outputting it directly. Change:
<% form_tag(:controller => "posts", :action => "create") do %>
to:
<%= form_tag(:controller => "posts", :action => "create") do %>
Check your routes file and make sure it is correct. And do a = sign in your 1st row. Think that might work. But when you have a form for resource, using form_for is recommended:
<% form_for #posts.each do |p| %>
<%= p.label :message %><br />
<%= p.text_area :message, nil, :size => "44x6" %><br />
<%= submit_tag("Update") %>
<% end %>
Also, avoid < br/ >. Style your form with css (divs,classes etc). Of course , you'll have to make #posts var in your controller for this to work:
#posts = Post.all
Hope this helps
Related
This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Passing only two variables between controller and view - best practice?
There is my action:
def list
#codes = Code.order("created_at")
#languages = Language.order('name').collect {|l| [l.name, l.coderay]}
end
There is my view(I removed some lines):
<% #codes.each do |code| %>
<div class="code">
<%= link_to code.title, :action => 'show', :id => code.id %>
<% if code.author %>
#<%= code.author %>
<% end %>
</div>
<% end %>
<%= render :partial => 'shared/error_messages', :locals => {:object => #code} %>
<%= form_for :code, :url => {:action => 'create' }, :html => {:multipart => true} do |f| %>
<%= f.text_field :title %><br />
<%= f.text_area :content %><br>
<%= f.select(:language, #languages, {:selected => 'text'}) %>
<%= f.text_field :author %><br>
<%= f.submit "Submit code" %>
<% end %>
There are 3 variables in it: #codes(list of posts), #code(current post, used in another action) and #languages.
My IDE writes:
At most two instance variables should be shared between controller and
view
This inspection warns if there are more than two instance
variables shared between a controller and a view. A controller should
only manage one instance variable, plus a second one for the
current_user variable.
Usually I share more variables between Controller and View(in PHP), sometimes 10+.
How it's done in Rails?
You can save an instance var by making languages a helper:
def languages
Language.order('name').collect {|l| [l.name, l.coderay]}
end
Its a guideline some developers follow some of the time.
But I would read up on Rails Routing a bit more. Understanding how Rails routing works should give you a better idea on how to structure your code.
http://guides.rubyonrails.org/routing.html
I modified your code a bit, not tested. But hopefully gives you some good ideas.
Controller:
def new
#code = Code.new
#codes = Code.order("created_at")
end
def create
#code = Code.new(params[:code])
if #code.save?
# Do your thing.
else
# render your :new action passing your #code variable
end
end
View:
<% #codes.each do |code| %>
<div class="code">
# Use Rails Routing - In console, type rake routes to get list of routes.
<%= link_to code.title, code_path(code.id) %> # example.
<% if code.author %>
<%= code.author %>
<% end %>
</div>
<% end %>
<%= render 'shared/error_messages', :object => #code %>
<%= form_for #code, :html => {:multipart => true} do |f| %>
<%= f.text_field :title %><br />
<%= f.text_area :content %><br>
# language_list = helper method.
<%= f.select(:language, language_list, {:selected => 'text'}) %>
<%= f.text_field :author %><br>
<%= f.submit "Submit code" %>
<% end %>
I asked a question similar to this one about a week ago, but this is a slightly different perspective on it. The nature of the question involves being redirected to the correct controller.
I have a single resource, posts, and I have 4 different categories these posts can be under. I want each of these categories to be particular to a single controllers, and so I have the following in my routes.rb:
resources "code", :controller => :code_posts, :as => :code
resources "sports", :controller => :sports_posts, :as => :sports
resources "gaming", :controller => :game_posts, :as => :gaming
resources "the-nation", :controller => :personal_posts, :as => :the_nation
So now I can access posts through URLs like, for example, /code/1, /sports/34 to access the same post resource, but with each controller focusing on a single scope, namely a particular category.
This is all well and good, but my issue comes up when I try to edit or save particular posts. I have the following partial _form.html.erb (rendered in the new and edit views) in all the view folders for their particular controller:
<%= form_for #post do |f| %>
<div class="field">
<%= f.label :author %><br/>
<%= f.text_field :author %>
</div>
<div class="field">
<%= f.label :title %><br/>
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :category %>
<%= f.select :category_id, Category.all.collect {|c| [c.name, c.id] }, {:include_blank => true} %>
</div>
<div class="field">
<%= f.label :summary %><br/>
<%= f.text_area :summary, :rows => 5 %>
</div>
<div class="field">
<%= f.label :body %><br/>
<%= f.text_area :body %>
</div>
<div class="field">
<%= f.label :tag_tokens %><br/>
<%= f.text_field :tag_tokens, "data-pre" => #post.tags.map(&:attributes).to_json %>
</div>
<div class="field">
<%= f.submit "Submit" %>
</div>
<% end %>
So whenever I create or update a post, through whichever controllers, I always get redirected back to /posts/4, /posts/123, /posts/:id, whatever. I want to get redirected to the particular controller the post being edited or created lives under. So if I go to /code/new, and submit the new post, I want to be redirected to /code/1234, and not /posts/1234. How can I do this? For some reason I'm just having major mental mind blocks this morning. Thanks.
EDIT Updated <%= form_for #post do |f| %> to <%= form_for #post, :url => code_url(#post) do |f| %> and it works for /code/1/edit but not /code/new. When trying to access a new post form, I get the following error:
No route matches {:action=>"show", :controller=>"code_posts", :id=>#<Post id: nil, author: "Les Peabody", summary: nil, body: nil, created_at: nil, updated_at: nil, title: nil, category_id: 1, slug: nil>}
This is my CodePostsController#new method
def new
#post = Post.new(:category => Category.find_by_name("Programming"), :author => current_user.full_name)
end
You may specify the url in the form
<%= form_for #post, :url => gaming_path do |f| %>
You may use inheritance on the model.
The path in you form is determined by the class name, and in this case it is post.
If they mach with resources naming it should generate proper paths as well.
The dirty hack may be keeping objects path in it, I saw someone do that, but I do not recommend it too much.
I think the reason is the form_for method which takes for the update action as default the name of the parameter (here post) it gets.
So to change that, you have to add at the beginning (for the example resource code) the following:
<%= form_for #post, :url => code_path(#post) do |f| %>
This is of course only the URL for an existing object, the URL for a new object should be different. It should be there new_code_path (and no argument). So your partial should only contain the fields and labels, not the form_for call, because the URL should be different then.
You should look at the output of the call in the shell: bundle exec rake routes and search for the correct paths in the output.
So, ultimately what is important is how the form gets turned into HTML. If you look at the differences between a form that is meant for editing, and a form that is meant for a new object, there is only one thing that is ever really different that matters - the action URL.
In the case of a new form, the form tag should look something like:
<form accept-charset="UTF-8" action="/code" class="new_post" id="new_post" method="post">
and in the case of an edit form:
<form accept-charset="UTF-8" action="/code/1" class="edit_post" id="edit_post_1" method="post">
The only thing that matters to rails however is the name of the input elements (which are constant in both forms) and the action attribute in the form tag. That tells Rails whether or not it's rendering the edit or create action.
Since we're splitting up control of a single resource through multiple controllers, the standard form_for #post will not suffice since Rails can no longer automate the rendering process through convention (as we're doing a very unconventional thing). It is necessary to do some manual labor. The following will do the trick.
Convert the partial to the following:
<%= form_for #post, :url => path do |f| %>
<div class="field">
<%= f.label :author %><br/>
<%= f.text_field :author %>
</div>
<div class="field">
<%= f.label :title %><br/>
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :category %>
<%= f.select :category_id, Category.all.collect {|c| [c.name, c.id] }, {:include_blank => true} %>
</div>
<div class="field">
<%= f.label :summary %><br/>
<%= f.text_area :summary, :rows => 5 %>
</div>
<div class="field">
<%= f.label :body %><br/>
<%= f.text_area :body %>
</div>
<div class="field">
<%= f.label :tag_tokens %><br/>
<%= f.text_field :tag_tokens, "data-pre" => #post.tags.map(&:attributes).to_json %>
</div>
<div class="field">
<%= f.submit "Submit" %>
</div>
<% end %>
The path variable in there is a variable passed in through the :locals mechanism in the partial render, like so:
new.html.erb
<%= render :partial => "form", :locals => {:path => code_index_path} %>
and edit.html.erb
<%= render :partial => "form", :locals => {:path => code_path(#post)} %>
The nice thing with this solution is you can DRY up the code too by placing _form.html.erb in app/views/layouts or app/views/posts and reuse it in all of the new and edit views for all controllers that manipulate the Post resource in a consistent fashion. So rather than having:
<%= render :partial => "form", :locals => {:path => code_path(#post)} %>
we have:
<%= render :partial => "layouts/form", :locals => {:path => code_path(#post)} %>
I have a form (snippet)
<% form_for(#transfer, :html => {:multipart => true}) do |f| %>
<p>
<%= f.label :source %><br />
<%= f.text_field :source %>
</p>
<p>
<%= f.label :destination %><br />
<%= f.text_field :destination %>
</p>
<% fields_for :upload do |u| %>
<p>
<%= u.label :upload %><br />
<%= u.text_field :upload %>
</p>
<% end %>
<p>
<%= f.submit 'Create' %>
</p>
<% end %>
<%= link_to 'Back', transfers_path %>
So now in my transfers controller I can do:
#transfer = Transfer.new(params[:transfer])
#upload = Upload.find_or_create_by_md5(params[:upload])
I am able to post to a single form with XML by simply changing the params to XML like
<transfer>
<source>foo</source>
<destination>bar</destination>
</transfer>
or
<upload>
<upload>baz</upload>
</upload>
But I cannot figure out how to combine them under the same XML root
Assuming you're using Rails 2.3.x, you might want to look into adding accepts_nested_attributes_for to your Transfer model. See what-s-new-in-edge-rails-nested-attributes.
Well I haven't been able to figure out how to do this for XML, so for now I've had to settle for doing it with REST. I came across the RestClient library and looking through the source, figured out you could do nested params like this:
RestClient.post( url,
{
:transfer => {
:path => '/foo/bar',
:owner => 'that_guy',
:group => 'those_guys'
},
:upload => {
:file => File.new(path)
}
})
Send a note to the author about documenting the functionality here
I've been trying to create an order confirmation page for my rails app, and am not quite sure how to go about it in a restful way.
There were a few answers on this question that got me halfway there, but the problem was that I wasn't quite sure how to set up the form in the rails view so that it would take the user to a confirmation page with all their details instead of a create action.
Right now my view is simple:
<% form_for :order do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :first_name %><br />
<%= f.text_field :first_name, :size => 15 %>
</p>
<p>
<%= f.label :last_name %><br />
<%= f.text_field :last_name, :size => 15 %>
</p>
(Be sure to enter your name as it appears on your card)
<p>
<%= f.label :card_type %><br />
<%= f.select :card_type, [["Visa", "visa"], ["MasterCard", "master"], ["Discover", "discover"], ["American Express", "american_express"]] %>
</p>
<p>
<%= f.label :card_number %><br />
<%= f.text_field :card_number %>
</p>
<p>
<%= f.label :card_verification, "Card Verification Value (CVV)" %><br />
<%= f.text_field :card_verification, :size => 3 %>
</p>
<p>
<%= f.label :card_expires_on %><br />
<%= f.date_select :card_expires_on, :discard_day => true, :start_year => Date.today.year, :end_year => (Date.today.year+10), :add_month_numbers => true %>
</p>
<p><%= f.submit "Submit" %></p>
What things should I be doing to direct the user to a confirmation page that shows all the order details?
Thanks!
Kenji
There were a few answers on this
question that got me halfway there,
but the problem was that I wasn't
quite sure how to set up the form in
the rails view so that it would take
the user to a confirmation page with
all their details instead of a create
action.
Directing a form to a non standard page is pretty simple.
Add a url option form_for.
Such that
<% form_for :order do |f| %>
becomes
<% form_for :order :url => {:action => "confirm"} do |f| %>
You'll need to crate the confirm action in your routes, but that only involves this:
map.resources :orders, :collection => {:confirm => :get}
All you need now is a basic controller action and a view:
def confirm
#order = Order.new(params[:order])
unless #order.valid?
render :action => :new
else
end
end
Your view should look almost identical to the show view, with the addition of a form submitting #order to the create action.
Why don't you pull the confirmation via ajax for example, pull the result and put it as an overlay div, upon confirmation submit the original values in the form.
If you still need to do it your way then check wizardly, it's exactly designed for such uses.
I would like to update the answer for more elegant Rails 4 or up.
I hope it will help newbies like me. Ruby is awesome! :)
routes.rb
resources :orders do
collection do
post 'confirm'
end
end
orders_controller.rb
def confirm
#order = Order.new(order_params) # GET THE POST parameters
render :new if #order.invalid? # Return if false
end
form.html.erb
<%= form_for #order, url: {action: 'confirm'} do |f| %>
I am attempting to use form_for to implement a search form that works with a table-less Search model I created. The search form keeps triggering the 'index' action. I assume I should use 'new' to create the form and 'create' the process the search query. Looking at the log, my POST is getting changed into a GET. Here's my code:
/searches/new.html.erb:
<% form_for :searches, #search, :url => searches_path, :html => {:method => :post} do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :keywords %><br />
<%= f.text_field :keywords %>
</p>
<p><%= f.submit "Submit" %></p>
<% end %>
What's the standard way for triggering the 'create' action with form_for?
Are you using the RESTful map.resources :searches ?
If so, shouldn't your :url be set to new_search_path ?
form_for is used with models. For a simple search form, I reccommend doing something like this:
<% form_tag posts_path, :method => :get do |f| %>
<%= f.text_field :query %>
<% end %>
You'll get /posts?query=wtf.