simple posts list with buttons - ruby-on-rails

Please help me to understand one dumb thing.
In one hand I have user with many posts, in other hand I have list with posts (every post have button) in view. On button I need to call custom method on current post with parameters. How to do this?

Try this:
link_to 'Submit', model_path(:query => "value"), :remote => true, :method => :post

Related

How to link to a class with selective text in ruby?

So, I'm trying to make a link which has a class and some text with the given link. Here's the code -
= link_to((session[:request_id] ? 'Save & Exit': 'Exit'), "/account/#{#name}", :id => 'cancel-application', :class => "cancel-application")
What I want is that I get a link to this class called "cancel-application", which is actually a small cancel image. In front of it, I want to get "Save & Exit" or "Exit" depending on the session. The "Save & Exit" or "Exit" button should also be linked. Something like this -
[cancel button] "Exit" (where both link to the same place)
Right now, what this code does, is put the image and the text on top of each other, which makes sense. So, what I thought of doing was put the session id in a variable and then put it after the link like this -
= #exit_text = session[:request_id] ? 'Save & Exit': 'Exit'
= link_to("/account/#{#name}", :id => 'cancel-application', :class => "cancel-application") #exit_text
This doesn't work. I was wondering how I could do something like that.
EDIT - I found a solution, although I feel there can be better ones.
You can still do this with one link:
= link_to "/store/#{#name}", :id => 'cancel-application' do
.cancel-application
= session[:request_id] ? 'Save & Exit' : 'Exit'
link_to can accept the block which is captured to build link's inner html. When you pass the block, you do not pass links label.
I just used two link_to. I guess there's no way to do it in one link_to:
= link_to("", "/account/#{#name}", :id => 'cancel-application', :class => "cancel-application")
= link_to((session[:request_id] ? 'Save & Exit': 'Exit'), "/account/#{#name}", :id => 'cancel-application')

Rails: Select helpers are sending get request

i'm currently getting an issue with this simple code :
=form_tag payments_path, :method => :post, :id => "braintree-payment-form" do
= select_month(14, :use_month_numbers => true)
= select_year(Time.now.year, :start_year => Time.now.year, :end_year => Time.now.year + 10)
In fact, when i select a value in one of the two selects above, it automatically to a get request on the current URL with the parameter i selected
exemple: if i click on 2013 in select_year it lead to /payments/2013
Anyone have a clue about it? It's exetremely annoying, i ran through this issue previouly and couldn't have found a proper solution.
Thanks!
UPDATE:
When i put these two selects out of the form tag, il also redirect me to /payment/selected_value, i really don't understand why it does that?
I tried put a e.preventDefault() in JS on the change of the selects but it has no effect

Correct HAML for linking an image to another page in my Ruby on Rails app

Trying to figure out how to use haml "link_to" with Ruby code in order to make an image on a page link to another page on the site. If possible I'd like to keep the nav static and fade the two pages in and out via a "Forward" and "Back" image. Any ideas? Just want to get the linking right first and then can go in and figure out the JQuery. Currently have the code below...
Thanks!!
.pad-bottom50
%div{:style => "position: absolute; top: 620px; left: 830px;"}
=link_to (image_tag(#page.photos[1].image_url(:full), :id => "#fade1", :class => "animated") if #page.photos[1].image?
.pad-top20
Syntax of link_to is
=link_to link_text, link_url, options
You missed the link_url. ie. to where the user should be taken when clicking on the image.
Here is a working example
=link_to(image_tag("http://goo.gl/FZUI3"), "http://en.wikipedia.org/wiki/Nyan_Cat", :id => "#fade1", :class => "animated")
You are not specifying the src for the link.
The syntax is:
link_to "Link Text", "/path-to-link"
To put an image in there:
image_tag "path-to-image"
link_to(image_tag("path-to-image"), "/path-to-link")
This code will make an image wrapped by a link pointing to /bacon if #page.photos[1].image?
link_to(image_tag(image_tag(#page.photos[1].image_url(:full), :id => "#fade1", :class => "animated")), "/bacon", :id => "bacon", :class => "bacon") if #page.photos[1].image?
You should first look at the parameters of a link_to tag . You will get to know about the parameters and what you can do with those parameters .
check this link
api doc
You can check all paramters of a method avilable in API Ruby on Rails

How do I build link with specific part of params hash from previous request?

While I wouldn't normally create a page like this, please know this is a current constraint I can't change.
The page has a checkbox form as well as a table with links for THs that sort the table. I need to construct the TH link in a way that it retains the checkbox items already checked.
Checkbox constructed in View with Haml as:
= form_tag movies_path, :method => :get do
Include:
- #all_ratings.each do |rating|
= rating
= check_box_tag "ratings[#{rating}]", "1", (#ratingsarray.include?(rating) ? true : false)
= hidden_field_tag 'sort', #sort
= submit_tag 'Refresh'
Then for the table it has this for the TH
%th{:class => #classrelease_date}
%a#release_date_header= link_to "Release Date", movies_path(:sort=>'release_date', :params[:ratings]=>params[:ratings])
Ultimately I want the URL like "/moves/?sort=release_date&Ratings[PG]=1&Ratings[G]=1" where I am spitting out the ratings params back to the page as part of the URL. Or how to I pass the ratings params in any part of page where the existing controller code will read it.
Existing controller code access ratings from checkbox:
params[:ratings]
Since movies_path accepts hash as parameter, you can tailor params and then generate the URL with movies_path(params). Generally, you may need to remove "controller" and "action" from params.

Cannot precheck check_box_tag in rails

I have code that looks like this:
#all_ratings.each do |rating|
= check_box_tag "ratings[#{rating}]", session[:checkbox][rating], :id => "ratings_#{rating}"
...
= submit_tag 'Refresh', :id => "ratings_submit"
By saving the state of which checkboxes were clicked I hoped to be able to pre-check the boxes that were clicked after the request had gone through and the page reloads. The problem that I am having is that the code above doesn't seem to work. I'm not sure if it's because I have the :id => "ratings_#{rating}" bit at the end (which is required for this assignment). I checked out the rails api here, but that was as clear as mud. Thanks in advance for the help!
Cheers
(Disclaimer: This code is for HW 2 for Coursera's Software as a Service course - I have finished the bulk of the logic for the HW, but this last bit is beyond me and seems to be more of an idiosyncracy than a major topic, hence I am posting it here.)
What boolean variable did you add? Considering I have a model with has_many ratings, say Video, and have #video defined, I'd do
= check_box_tag "ratings[#{rating}]", session[:checkbox][rating], #video.ratings.include?(rating), :id => "ratings_#{rating}".
The #video.ratings.include?(rating) part prechecks the ratings associated to the current video. What are the associations related to your Rating model?

Resources