I'd like to delete multiple objects of the same type using a RESTful controller.
The most simple thing I can think of is to have the destroy action expect a comma-separated list of ids of objects to destroy.
Is there a more elegant way to do this?
I think it would be more elegant to take an array of ids:
http://guides.rubyonrails.org/action_controller_overview.html#hash-and-array-parameters
You could use nested forms for it..
See http://ryandaigle.com/articles/2009/2/1/what-s-new-in-edge-rails-nested-attributes
I think thats the most elegant version...
<% form_for #person do |person_form| %>
<%= person_form.label :name %>
<%= person_form.text_field :name %>
<% person_form.fields_for :children do |child_form| %>
<%= child_form.label :name %>
<%= child_form.text_field :name %>
<% unless child_form.object.new_record? %>
<%= child_form.check_box '_delete' %>
<%= child_form.label '_delete', 'Remove' %>
<% end %>
<% end %>
<%= submit_tag %>
<% end %>
Here's how the RESTful request might look.
POST /posts/delete_multiple HTTP/1.1
Host: www.example.com
post_ids[]=33&post_ids[]=47&post_ids[]=88
Note that while GET, PUT, and DELETE have very specific meanings in the context of REST, POST is more vague and essentially means to take some action. The action to take is given in the URL and additional data specific to the action are passed in the entity (body) of the request. Only use POST in this manner when GET, PUT, and DELETE do not have the intended meaning.
POST is commonly interpreted as "create", but this is not really correct. We commonly use POST for creating new resources when the client doesn't know what the URL of the newly created resource should be. But when the client does get to determine the URL of the newly created resource, the correct verb would be PUT.
Related
In my Rails 6 app, I have a Product and Order model.
On my products#show page, I have some fields and a button. What I need to do is send the info to the orders#new page so that this data is shown on the orders#new page.
I have tried to write some code based on Pass variables without model associations in Rails and how to pass parameters in params using form_tag method in rails, but my code seems completely wrong.
on products#show:
<%= form_tag(new_order_path do |form| %>
<%= form.input_field :comments %>
<%= form.button %>
<% end %>
With this code I get undefined method text_field' for nil:NilClass`.
I have tried adding attr_accessor :comments to both the Product and Order model, but it doesn't help.
I don't think that my approach or what I am trying to code is right. I was just trying to piece together parts from these answers.
Can someone please help me figure out the best way I can pass this data to Orders#new to show in that view?
<%= form_with url: "/search", method: :get do |form| %>
<%= form.label :query, "Search for:" %>
<%= form.text_field :query %>
<%= form.submit "Search" %>
<% end %>
from the docs
https://guides.rubyonrails.org/form_helpers.html
and the show path also requires an id
I have in rails the following form in a view
<%= form_for (#account) do |f| %>
<%= f.label :comments,"Comments" %>
<%=f.text_area :comments %>
<%= f.submit "Confirm",:name=>"conf" %>
<%= f.submit "Reject" %>
<% end %>
When I submit the form I get the following hash in the log before the update of the database
Started PATCH "/accounts/12" for 127.0.0.1 at 2015-08-13 21:31:18 +0200
Processing by UseractionsController#answer_with_comments as HTML
Parameters: {"utf8"=>"✓", "account"=>{"comments"=>"mycomments"}, "conf"=>"Confirm", "id"=>"12"}
I am trying to access the input in the comments text area in the controller. I tried
params[:account][:comments]
but it does not seem to work. Could anyone give me the appropriate syntax? Thanks.
EDIT
This is my controller code. Right now the if loop return false and nothing is added to the database even though there is something submitted ("mycomments" see above in the param nested hash)
if params[:bankaccount][:comments]
#bankaccount.update_attribute(:comments, params[:bankaccount][:comments])
end
It is only the appropriate syntax for your view. It assumes that you have content field on your Comment model.
<%= form_for (#account) do |f| %>
<%= f.label :comments,"Comments" %>
<%= f.fields_for :comments do |ff| %>
<%= ff.text_field :content %>
<% end %>
<%= f.submit "Confirm",:name=>"conf" %>
<%= f.submit "Reject" %>
<% end %>
You also will have to declare nested attributes in your Account model and your params hash should be different.
You should watch these two Railscasts part 1 and part 2 to learn more about nested attributes.
Since you mention strong parameters as a tag you probably want to build this a bit differently.
private
def account_params
#the permit method might need to be altered depending on your model and view
params.require(:account).permit(:comments)
end
Somewhere else in your controller you would then do:
#bankaccount.update_attributes(account_params)
Please take a read: http://edgeguides.rubyonrails.org/action_controller_overview.html#strong-parameters
I'm following this tutorial - http://tutorials.jumpstartlab.com/projects/blogger.html#i2:-adding-comments. It is building a sample blog app and so far has an Article model and is adding a Comment model. An Article has_many Comments.
They want to be able to create the comment using a form on the articles page:
<h3>Post a Comment</h3>
<%= form_for [ #article, #comment ] do |f| %>
<p>
<%= f.label :author_name %><br/>
<%= f.text_field :author_name %>
</p>
<p>
<%= f.label :body %><br/>
<%= f.text_area :body %>
</p>
<p>
<%= f.submit 'Submit' %>
</p>
<% end %>
I don't understand how this form works though. It binds two objects to the form, and it says that it'll submit to article_comments_path, which will POST to comments#create.
How does this work? How does it know what to do when it has two objects? Does the order of the objects in the array matter? Can you have more than two objects?
you are probably going to use nested resources:
resources :articles do
resources :comments
end
[ #article, #comment ] is an array which will build a nested route. Like:
/articles/1/comments
article_comments_path maps to the index action (GET) and to create action(POST)
dorake routes to see what i'm talking about:
article_comments_path GET /articles/:article_id/comments(.:format) comments#index
POST /articles/:article_id/comments(.:format) comments#create
So essentially, you use [ #article, #comment ] when you have nested resources to create routes helpers. the order of the objects matter as it is article_comments_path and NOT comment_articles_path.
whether you can bind more than 2 objects to the form, it depends, I think, on how deeply nested are your resources
UPDATE:
you are using a form. and forms are for either to create or update objects depending on whether the object exists or not. The submit button will trigger that. This article sums it pretty good
"When the user clicks submit on a form, Rails invokes either the create (for a new record) or update (for an existing record) action in the associated controller. Forms are, by default, submitted with an HTTP POST command, and all the information that was entered in the form is provided in a hash called params, which is available to the controller."
I am trying to accomplish something like this:
I am creating a simple blog. I have set up categories for my blog.
I want that when my user goes to posts/index, he sees a list of all categories.
Example:
Text
Image
Upon clicking on a category, my user gets redirected to the posts/new page, where the category_id field will by transmitted through a hidden_field.
So my code right now is:
in posts/index
<% #categories.each do |c| %>
<%= link_to c.name, new_post_path(:category => c.id) %><br />
<% end %>
and in my posts/_form i'm trying to do something like this
<%= f.hidden_field :category_id, :value => params[:category_id] %>
which is not working though, because the html output is
No value is being passed.
What is the correct way to proceed here?
Thx!
At first glance it looks like a simple mistake mixing up the param names category and category_id.
Try this:
<% #categories.each do |c| %>
<%= link_to c.name, new_post_path(:category_id => c.id) %><br />
<% end %>
Also, from what i can understand in your code, it seems a post belongs to a category. In such case, you could nest routes from one in another, and paths for creating nested object would become accessible, such as new_category_post(#category).
The routing would look like that:
resources :categories do
resources :posts
end
You can read about this matter here: http://guides.rubyonrails.org/routing.html
What is the difference between form_for and form_tag? Is anything different for form_remote_for and form_remote_tag?
You would use form_for for a specific model,
<% form_for #person do |f| %> # you can use f here
First name: <%= f.text_field :first_name %>
Last name : <%= f.text_field :last_name %>
<% end %>
Form_tag create basic form,
<%= form_tag '/person' do -%>
<%= text_field_tag "person", "first_name" %>
<% end -%>
form_for prefers, as its first arg, an activerecord object; it allows to easily make a create or edit form (to use it in a "new" view you should create an empty instance in controller, like:
def new
#foo = Foo.new
end
It also passes a form variable to the block, so that you don't have to repeat the model name within the form itself. it's the preferred way to write a model related form.
form_tag just creates a form tag (and of course silently prepare an antiforgery hidden field, like form_for); it's best used for non-model forms (I actually only use it for simple search forms or the like).
Similarly, form_remote_for and form_remote_tag are suited for model related forms and not model related forms respectively but, instead of ending in a standard http method (GET, POST...), they call an ajax method.
All this and far more are available for you to enjoy in the FormHelper and PrototypeHelper reference pages.
EDIT 2012-07-13
Prototype has been removed from rails long ago, and remote forms have completely changed. Please refer to the first link, with reguard to the :remote option of both form_for and form_tag.
These should be similar:
<% form_for #person do |f| %>
<%= f.text_field :name %>
<% end %>
and:
<%= form_tag '/person' do %>
<%= text_field_tag "person[name]" %>
<% end %>
If you want to submit the same params to the controller, you would have to define this explicitly.