Rails form multiple params XML - ruby-on-rails

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

Related

Creating a Form (Ruby on Rails)

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

Routing for multiple controllers for a single resource in Rails 3.1

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)} %>

Difficulty in getting mp3 to work in Paperclip

I am trying to get Paperclip to accept an mp3 file. I was able to get this to work in a Rails 2 app, but am having difficulty in Rails 3. I can get the file to show up in my assets directory, but I continue to get the missing.png listed instead of the appropriate file in the show action.
Here is my model code...
has_attached_file :sermonfile,
:url => "/assets/:class/:id/:style/:basename.:extension",
:path => ":rails_root/public/assets/:class/:style/:id/:basename.:extension"
attr_accessor :sermonfile_file_name attr_accessor
:sermonfile_content_type
attr_accessor :sermonfile_file_size
attr_accessor :sermonfile_updated_at
Here is the form view...
<%= form_for #sermon, :html =>
{:multipart => true} do |f| %> <% if
#sermon.errors.any? %>
<%= pluralize(#sermon.errors.count,
"error") %> prohibited this sermon
from being saved:
<ul>
<% #sermon.errors.full_messages.each do
|msg| %>
<%= msg %>
<% end %>
<% end %>
<%= f.label :title %>
<%= f.text_field :title %>
<%= f.label :permalink %>
<%= f.text_field :permalink %>
<%= f.label :speaker %>
<%= f.text_field :speaker %>
<%= f.label :date %>
<%= f.date_select :date %>
<%= f.label :series %>
<%= f.text_field :series %>
<%= f.label :book %>
<%= f.text_field :book %>
<%= f.label :passage %>
<%= f.text_field :passage %>
<%= f.label :notes %>
<%= f.text_area :notes, :class => "mceEditor" %> <%= f.file_field
:sermonfile %>
<%= f.submit %> <% end %>
This is what I am using to render the file in the show view...
<%= link_to #sermon.sermonfile.url %>
Any assistance is greatly appreciated!
Your :url & :path have their :id & :style mixed up. They should be the same: :id/:style
has_attached_file :sermonfile,
:url => "/assets/:class/:id/:style/:basename.:extension",
:path => ":rails_root/public/assets/:class/:id/:style/:basename.:extension"

ruby on rails photo upload problem

Hallo
rails version 2.3.5
I'm learning rails and I run into a problem.
I'm doing some nesting forms from the railscasts tutorials. I changed the text area into a data field to upload photos and everything is working.
Now i have to display the uploaded pictures and i simply can't do it. I Tried everything I could find on the net but nothing worked.
PROBLEM
I have the Article controller which handles the article CRUD.
inside the article new form there is nested a form for uploading images.
article controller
def code_image
#image_data = Photo.find(params[:id])
#image = #image_data.binary_data
send_data(#image, :type => #image_data.content_type,
:filename => #image_data.filename,
:disposition => 'inline')
end
photo model
def image_file=(input_data)
self.filename = input_data.original_filename
self.content_type = input_data.content_type.chomp
self.binary_data = input_data.read
end
articles/show.html.erb
<%=h #article.title %>
<%=h #article.body %>
<% for photos in #article.photos %>
<%= image_tag(url_for({:action => 'code_image',
:id => #article.photos.id})) -%>
<% end %>
articles/_formnew.html.erb
<% form_for (:article, #article,
:url => {:action=>'create'}, :html=>
{:multipart=>true}) do |f| %>
<%= f.label :title %><br />
<%= f.text_field :title %><br /><br />
<%= f.label :body %><br />
<%= f.text_area :body, :style => 'width: 600px;' %><br /><br />
<% f.fields_for :photos do |builder|%>
<%= builder.label :content, "Photo"%><br />
<%= builder.file_field :image_file %><br />
<% end %>
<br />
<%= f.submit "Create" %>
Thanks
Old question I know, but if somebody stumbles across it, this page helped me:
http://sleekd.com/general/adding-multiple-images-to-a-rails-model-with-paperclip/
Also, to display the image, you can just use:
image_tag [model].[paperclip_attachment].url
eg:image_tag trip_image.photo.url

Order confirmation page in rails

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| %>

Resources