link-to

Rails link_to assigning class and id

故事扮演 提交于 2019-12-10 03:59:45
问题 <%= link_to event do %> #bunch of stuff making up the partial. <% end %> So I'm trying to assign an ID and a class to each item in a partial. I've seen where you have to call the full link_to function like <%= link_to event, { controller: :controller, action: :action }, {class: 'someClass', id: 'someId' } %> . That's not working for me, because of the do block, methinks? Ideas? 回答1: Does this work for you? <%= link_to event, id: "an-id", class: "some-class" do %> #bunch of stuff making up the

Rails: How can I show a block with or without a link based on a condition (link_to_if)

心已入冬 提交于 2019-12-10 03:22:33
问题 I have a complex block of tags ( <h3> , <p> , ...) that I want to render with a link or without a link around it based on a condition. I know about link_to_if that works like that: <% link_to_if condition, name, path %> if the condition is false only the name will be rendered. And I know about the link_to with &block : <% link_to path do %> [complex content] <% end %> I want a combination of both. A link_to_if statement that accepts a &block , so that the block will be rendered without a link

How to add a link_to in a select_tag with Rails/ERB?

烂漫一生 提交于 2019-12-10 03:19:03
问题 I have the following code in my index.html.erb <%= select_tag 'team_id', options_for_select(@teams.map{|team| ["#{team.name} # {team.nick}", team.id] }) %> Where would I add a link_to helper within that block? Can I even add a link_to for select_tag? The desired link_to would go to '/Teamleader/ID_OF_OPTION_PICKED' UPDATE: To be more clear; when a user selects an option from the select tag, I want to redirect the page to the desired link (from link_to). 回答1: <%= select_tag 'team_id', options

:confirm in rails not working

无人久伴 提交于 2019-12-10 01:02:33
问题 I just started coding in ruby on rails and I've been following a guide which is using a more outdated version of rails than I am using. I am using 3.2.12 This is my code: <%= button_to 'Destroy', product, :method => "delete", :confirm => 'Are you sure?' %> From what I understand, these are symbols that are passed to rails, which is then converted to either an html or javascript action that then pops up the message box and deletes the object if applicable. The above code destroys the object,

Button_to in Ruby on Rails bad route

南笙酒味 提交于 2019-12-09 18:09:30
问题 I'm trying to use the button_to rails helper. I wrote the following code: <%= button_to 'Edit Item', edit_item_path(@item), :class => 'mark-button' %> and got the following error message No route matches "/items/1/edit" But when I refresh the page it goes to the appropriate action. The URL of the page i get is localhost:3000/items/1/edit which is the correct URL. If I switch the button_to command to link_to the page loaded with no errors. Meaning this code: <%= link_to 'Edit Item', edit_item

Rails 'link_to' to Download An Image Immediately Instead of Opening it in the Browser

对着背影说爱祢 提交于 2019-12-08 23:18:07
问题 I have a link_to Rails helper that downloads a wallpaper when clicked. But the image is loading in the browser instead of being downloaded immediately. <%= link_to "1920x1080", @download.wallpapers[1].wallpaper.url %> But at the same time I have a link_to Rails helper that downloads a screensaver of .exe format but here it works as inteded: file being downloaded immediately. <%= link_to "720p", @download.screensavers.first.screensaver.url %> What should I add or do so that the images will not

Rails: Call controller action with link_to an external link (and update a join-table)

冷暖自知 提交于 2019-12-08 12:01:59
问题 In my rails app there are posts which has links to other external pages ( @post.link gives me the link). If the current_user clicks on the link, a controller action should be triggered, namely the action viewed , which should update my database, more specifically a join-table with the information that the user has viewed the link. What I've already done so far: In my views/posts/index.html: # Looping through all the posts <%= link_to post.title, post.link, target: "_blank", controller: "posts

What objects should be passed to a link_to with triple nested route?

五迷三道 提交于 2019-12-08 10:50:38
问题 What objects should I pass to my link_to for a triple nested route? I want to retrieve the exercise show page. show.html.erb - workouts <%= link_to exercise.name, plan_workout_exercise_path(???) %> routes.rb resources :plans do resources :workouts do resources :exercises end end workouts_controller.html.erb def show @workout = Workout.find(params[:id]) end I have attempted the following, but it doesn't feed the right ID to the right model. <%= link_to exercise.name, plan_workout_exercise_path

rails - how to change database values with a link

我的梦境 提交于 2019-12-08 06:36:01
问题 I am making a website using rails and I am in desperate need of help of how do I create a link that when clicked on it will update an attribute in the database but only when clicked on.I have this code: <%= link_to (myproperty_path) do %> <% @player.update_attribute("energy", @player.energy + 2) %><span>Take a Nap</span> <%end%> but the problem with this is that whenever I refresh the page it updates the attribute and when I go to another page on the website it updates the attribute again. It

What's the correct way to do ajax callback with Rails 3 link_to [duplicate]

倖福魔咒の 提交于 2019-12-07 15:39:55
问题 This question already has answers here : Rails 2 to Rails 3 : using link_to instead of link_to_remote (including remote and update) (2 answers) Closed 3 months ago . Here's the scenario. Summary: I have a solution, but I want to know if this is the best way to get an AJAX callback working in Rails 3. Problem: I have a link that when clicked it should use AJAX to update an existing html element (in my case a div ) on the page: <div id="project_content">Change Goes here</div> My Solution In my