How to create a link that will update a column - ruby-on-rails-2

I'm using Rails 2.3.8.
How do you create a link that will update a column?
So, I'm trying <%= link_to 'stuff', :controller => controller, :action => action, :id => id %>
The action method will just update the column of the row associated with :id, so I don't want it to redirect to controller/action page. I just want the column updated and call save on the object.

Never use a GET to update anything, always use a POST. Meaning: A link is bad practice and a security risk.

Related

redirect to another URL using submit button in ruby

I have submit button and i want to redirect in another URL (hard coded) this URL
https://www.ccavenue.com/shopzone/cc_details.jsp
my code :
<%= form_tag #ccavanue do |f| , url => "https://www.ccavenue.com/shopzone/cc_details.jsp", :html => :method => "put" %>
<%= hidden_field_tag :my_field, #MerchantId, :id => 'merchant_id' %>
<%= submit_tag "Click Me" %>
<% end %>
i want to redirect another website URL with this submit button . please guided me.
Change your code to following:
<%= form_for #ccavanue, url: "https://www.ccavenue.com/shopzone/cc_details.jsp" do |f| %>
<%= f.hidden_field :my_field, #MerchantId, :id => 'merchant_id' %>
<%= f.submit "Click Me" %>
<% end %>
In Rails a form is designed to create or update a resource and reflects the identity of the resource in several ways:
The url that the form is sent to (the form element's action attribute) should result in a request being routed to the appropriate controller action (with the appropriate :id parameter in the case of an existing resource),
Input fields should be named in such a way that in the controller their values appear in the appropriate places within the params hash, and
For an existing record, when the form is initially displayed, input fields corresponding to attributes of the resource should show the current values of those attributes.
In Rails this is achieved by creating form using form_for where:
If we want to create any object we use POST method within url and PUT method if we are trying to update an existing record.
Rails framework is smart enough to use POST or PUT method by itself looking at the url of the form. So in this case we need not use method parameter within form_for url.
Probably you can start with Michael Hartl's tutorial

Rails 4 - Controller Edit Action Returning Wrong Record

I have a Comment model which belongs to both User and Story. Creating a comment correctly associated to the appropriate User and Story is working fine but when trying to edit the comment my edit action appears to retrieving the wrong record.
The offending action in comments_controller.rb:
def edit
#story = Story.find_by(params[:story_id])
#comment = #story.comments.find_by(params[:id])
end
The link used to render the comments/edit view:
<%= link_to 'edit', edit_story_comment_path(comment.story_id, comment.id) %>
The corresponding view:
<%= form_for(#comment, url: { controller: 'comments', action: 'update' }) do |f| %>
<%= f.text_area :content %>
<%= f.submit "update" %>
<% end %>
The edit view appears to be rendering the most recently added comment regardless of which #comment I am trying to edit.
You're using find_by, which is basically a magic find_by_X method, with no fields specified. find_by(1) generates invalid SQL for me using Postgres, but it might be that whatever database back-end your using accepts it.
Regardless, find_by certainly won't do what you want it to do.
You should be using find, if you want to find records by id:
#story = Story.find(params[:story_id])

how to use pass paramters to controller every time an item is selected in select tag in rails 3?

this my select statement, I guess I cannot figure out how to make it work:
<%= select("post", {project_id: #project, word_id: #word_id},
#words[count].synonyms.all.collect {|syn| [ syn.name ] ,
{:id=>"Menu1_Id", :class => "Menu1_Class", :onchange => "alert('hey')"}) %>
#project and #word_id are the parameters I want to make it pass to the controller,
where to add my url path ?

Rails: Ajax-enabled form without a model object

I'm new to Rails and having a hard time figuring out how to create a form that submits over Ajax without having a corresponding model object.
My use case is a simple form that collects an email address and sends it an email; there's nothing to be persisted, so no model.
In cases where I do have a model, I've had success with form_for(#model, remote: true). I can't seem to find the right helper for the case where there is no model. I tried form_tag(named_path, remote: true) and that works, but does not use Ajax.
Pointers to an example with an example with a proper controller, .html.erb and routes.rb would be really appreciated.
Here's how I solved it. The key was using form_tag and specifying a hash with an argument that properly matched my route. For my business logic, I need to pass in an "id" param, which is used in the controller logic.
<%= form_tag(sendmail_path({ id: 1}), remote: true) do %>
<%= text_field_tag :email, params[:email] %>
<%= submit_tag "Send", remote:true %>
<% end %>
routes.rb:
MyApp::Application.routes.draw do
post '/sendmail/:id(.:format)' => 'mycontroller#sendmail', :as => 'sendmail'
# etc...
end
I also had to supply a sendemail.js.erb template which updates the form after submission.

How to specify a controller & action for Ajax pagination with Kaminari?

I'm using the Kaminari gem for my ruby on rails application (For anybody still using will_paginate, I would recommend to consider switching! Much cleaner and more versatile).
The problem I have is that I want to specify a controller action when doing AJAX pagination, ie. get my pagination to work remotely, as such:
<%= paginate #feeds, :param_name => :page, :remote => true%>
By default, when I set :remote => true, and add the following lines to my "pages" controller home.js.erb file, it works on my homepage. I don't even need to specify which js partial to refer to; it chooses automatically.
$('.synopsis').html('<%= escape_javascript render('exchanges/synopsis', :feeds => #feeds)%>');
$('.paginator').html('<%= escape_javascript(paginate(#feeds, :param_name => :page, :remote => true).to_s)%>')
BUT the catch is that this "synopsis" div is a partial that I render on a lot of different pages in my application. So on pages where the controller isn't my "pages" controller, the remote pagination no longer works.
One solution would be to add these two lines to js.erb files in my many different controllers. But that would be redundant and inelegant.
I would prefer to be able to specify in the "paginate" call, which controller and which action to use for the remote call. Then I think I would write an action like:
def paginate_remotely
respond_to paginate_remotely.js
end
How would I be able to do this? It seems like such a simple question but I haven't been able to figure it out. The following certainly does not work:
<%= paginate #feed_exchanges, :param_name => :exchange_page, :remote => true, :controller => :exchanges, :action => :paginate_remotely %>
For those who are interested, the answer is you have to specify the controller and action under a :params specification, as follows:
<%= paginate #feed_exchanges, :params => {:controller => :exchanges, :action => :paginate_remotely}, :param_name => :exchange_page, :remote => true %>
Don't forget to write define the route for this action in config/routes.rb, and create a js partial, in my case called paginate_remotely.js.erb, containing:
$('.synopsis').html('<%= escape_javascript render('exchanges/synopsis', :feeds => #feeds)%>');
$('.paginator').html('<%= escape_javascript(paginate(#feeds, :param_name => :page, :remote => true).to_s)%>')

Resources