Pretty new to Ruby on Rails and reached my first road block I can't seem to figure out after some researching.
On my view, I am generating a button for each instance of "Newbie"
I would like to send the 'zendeskid' of each to my controller "giveticket"
So far, I have:
<div class="container">
<div class="containertop"></div>
<p><%= newby.name %></p>
<%= button_to "Give Ticket!", {:controller => "giveticket",:action => "new", :newby.zendeskid => #newby.zendeskid} %>
</div>
</div>
<% end %>
however, I am getting "undefined method `zendeskid' for :newby:Symbol"
Could someone please point me in the right direction - a bit stuck at the moment! Thank you in advance.
To send any parameters to the controller action you can just send it with the action name.
Named routing is more preferable and recommended to use in rails. So you can write as,.
<%= button_to "Give Ticket!", new_giveticket_path(zendeskid: newby.zendeskid) %>
Related
Help! I'm relatively new to Ruby on Rails and now I'm attempting to use Rails 7 with Turbo and Stimulus to update a partial on my web page.
I have buttons on a page within a partial (call it _home.html.erb). When clicked I'd like them to replace _home.html.erb with a different partial.
Let me show you what I've done so far:
view:
index.html.erb
<%= turbo_frame_tag "main-speaker-div" do %>
<%= render partial: 'home' %>
<% end %>```
_home.html.erb
<div class="row">
<div class="col-5">
<%= button_to "Settings", rendersettings_path(), remote: true, class: "reader-general-button"%>
</div>
</div>
controller:
def rendersettings
broadcast_replace_to "main-speaker-div", partial: 'settings'
end
This code is throwing the following error:
POST 500 http://localhost:3000/rendersettings (Internal Server Error)
Response has no matching element
Server console error:
NoMethodError (undefined method `broadcast_replace_to' for #HreaderController:0x0000000002be30):
Thanks in advance for your help!
method broadcast_replace_to is supposed to be called on an object of Model. I guess in your case you must have an Object of header, and this method would be called as #header.broadcast_replace_to
I actually ended up doing the following to with my view to fix this issue:
<%= turbo_stream_from "main-speaker-div" %>
<%= turbo_frame_tag "main-speaker-div" do %>
<turbo-frame id="main-speaker-div">
<%= render partial: 'homefull' %>
</turbo-frame>
<% end %>
It seems like if I am broadcasting I would need to have the turbo_stream_from tag added as a listener. Please point out any issues you see with this! I'm basically doing trial and error since this is so new to RoR
So im still very new to ruby on rails and what Im trying to do here is very simple. Im trying to create a facebook like app where if you click on the profile picture of a post it will direct you to the users profile page. I have just done something really similar in a online course but I cant seem to get this one to work in another view. Here is what I have that works in my header, navbar.
NAVBAR
<nav class="navbar navbar-default navbar-fixed-top">
......
<li><%= link_to "Show Profile" ,
user_profile_path(current_user.id, current_user.full_name) %></li>
....
</nav>
This code works and directs me to the corresponding users profile page.
Routes
Rails.application.routes.draw do
....
root 'statuses#index'
get '/:id/:full_name', to: 'profile#show' , as: :user_profile
.....
end
Problem View
<div class="page-header">
....
<% #statuses.each do |status| %>
<div class="row">
<div class="col-md-1">
<%= link_to image_tag(status.user.avatar.url(:thumb),
user_profile_path(status.user.id, status.user.full_name)) %>
//the above is what gives me the error in the title.
</div>
<% end %>
I have done my fair share of searching around and it seems that this error occurs if im passing in strings when it accepts hashkeys? Im not entirely sure. If there is a better way to do this that I should use please show me as I am very new and open to learning.
You have a wrong usage of a link_to helper. Instead of:
<%= link_to image_tag(status.user.avatar.url(:thumb), user_profile_path(status.user.id, status.user.full_name)) %>
^first argument ^second argument
Use:
<%= link_to user_profile_path(status.user.id, status.user.full_name) do %>
<%= image_tag(status.user.avatar.url(:thumb)) %>
<% end %>
As you can see, you pass _path helper as the second argument to the image_tag, this is wrong. The second argument to the image_tag should be a hash, thats why you have a undefined method 'symbolize_keys' for errors.
EDIT: SOLVED, view the solution in Brian Kunzig's answer+comments.
I've decided to try out Ruby on Rails and have been constantly running into this problem. I was searching around for problems like this but none of the solutions have done the trick for me. It seems that I constantly write a part of code that just isn't correct. So here are some code snippets:
(ignore what the code would be for, it's just to try stuff out)
My controller:
class AuthsController < ApplicationController
def index
#auths=Auth.all
end
def new
#auth=Auth.new
end
def show
#auth=Auth.find(params[:id])
end
end
My index.html.erb:
<div style="margin: 0 auto; width:50%; text-align: center">
<h1>Please authorise your use of this webpage and its database(s).</h1>
<%= form_for :auth, url: auths_path do |f| %>
<% if #auth.errors.any? %> ==> RAILS REPORTS ERROR IN CODE HERE
<div id="error_explanation">
<h2>
<%= pluralize(#auth.errors.count, "error") %> prohibited
this authentification from being saved:
</h2>
<ul>
<% #auth.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<p>
<%= f.label :username %><br>
<%= f.text_field :username %>
</p>
<p>
<%= f.label :password %><br>
<%= f.text_field :password %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
</div>
So, bottom line. Rails is saying that I cannot use #auth in the form_for block, for example. Or anywhere else for that matter.. It always says that it belongs to NilClass or something like that. It obviously wants me to instantiate it somehow, but isn't it enough to make the method new and put in the line: #auth=Auth.new ?
I'm just confused with this situation because I can't figure out how it's supposed to go. Thanks a lot !
P.S. I'm using <%= form_for :auth, url: auths_path do |f| %> because it won't accept #auth, that's what the error in the next line is. I have seen solutions to instantiate it "on the go" outside of the controller but I want to do it the way it's supposed to be done.
You should be putting this form in the new.html.erb file and not the index. The index is for listing entries while the new and create actions handle POST requests. You're getting an error because you're trying to list all Auth's when none have been created. Also, you're sending a form via a GET request if you're using standard rails routing. Use resourceful routing and put this form in your new view for that controller and it should work.
Routes file should be:
resources :auths
This will provide all the necessary routing automagically. If you type rake routes after modifying this you will see the newly generated urls and the helpers to them. You will notice it affords the create and update actions a POST/PUT request while others are GET.
I am looking to learn a few things with Ruby on Rails and was wondering how I can make a basic calculator that doesn't touch the model in rails.
I am using form_tag
This is my main page
<%= form_tag do %>
<%= label_tag('first number') %>
<%= number_field('first_number', value = nil) %>
</br>
<%= label_tag('second number') %>
<%= number_field('second_number', value = nil) %>
</br>
<%= submit_tag("calculate") %>
<% end %>
<% first_number * second_number %>
I am getting an error that says :
undefined local variable or method first_number
How would I got about fixing this? I am not sure where to go from here?
Create a controller method. Then in the form_tag give the method's url. Then receive the parameters in that method. Then calculate and show the result.
Suppose,
Your current view calculator.html.erb show the form.
in the form tag use <form action="<%= calculate_result_path %>" method="post">
Here, calculate_result is a method in any controller.
def calculate_result
#result = params[:first_number] * params [:second_number]
end
Now make a view file for this method to show the result. Or you can ajaxify to show the result on the calculator.html.erb file.
To Ajaxify
suppose, you want to show the result in calculator.html.erb as like
<div class="show-result">
<%= #result %>
</div>
now create a calculate_result.js.erb for the method calculate_result. On that JS file replace the div with class show-result using a custom div where you put your result which you get from the controller method.
To learn more about, please learn how to ajaxify your views in rails. I'll recommend you to read the book "Agile Development"
I am trying to use link_to in rails 4 with controller and html options and a do..end block. I have seen similar posts but have not been able to use any of the answers successfully.
Working code without a do..end block:
<%= link_to 'recommend', { controller: 'recommendations', id: offer.id }, method: :post %>
When I try to use some embedded ruby to add extra information to the link, I cannot get it to work:
<%= link_to( { controller: 'recommendations', id: offer.id }, method: :post) do %>
<p>Some Html</p><%= offer.recommendations %>
<% end %>
The code compiles but in the rendered, the link that is generated is the following:
<a controller="recommendations" id="38">
<p>Some Html</p>0
</a>
Any help would be appreciated. I think that it is a small problem with the syntax but I have tried all manner of brackets, spaces etc that I could think of without luck.
UPDATE: I have tried the following code without success:
<%= link_to( { controller: 'recommendations', action: 'create', id: offer.id }, method: :post) do %>
<p>Some Html</p><%= offer.recommendations %>
<% end %>
The HTML output is:
<a action="create" controller="recommendations" id="39">
<p>Some Html</p>0
</a>
This might not be important but as a side note, the create action doesn't have a helper function for links. When I run the
rake routes
command I get the following
...
recommendations GET /recommendations(.:format) recommendations#index
POST /recommendations(.:format) recommendations#create
new_recommendation GET /recommendations/new(.:format) recommendations#new
...
In my opinion this isn't a problem but it is a reason why code such as:
link_to create_recommendation_path
won't work. Finally, the intention of the link is to act as a 'like' button. It creates a recommendation and then displays the current page again. Once again, thanks for the help in advance.
The reason link_to create_recommendation_path doesn't work is because there is no named route for create_recommendation_path, only for recommendations_path. You can see the named routes in your routes list (which you have in your post above). The left most column that comes out of routes shows the named routes. Notice that recommendations#create doesn't have an entry in the list.
You could probably get the path you want with
<%= link_to recommendations_path(:offer_id => offer.id), :method => :post do %>
html stuff
<% end %>
This should post to a path that looks like
/recommendations?offer_id=<the offer id>
(except the post data will be in the headers not on the URL)
This will work if the create method going to do something like
Recommendation.create(params)
and the only parameter you need to create a new Recommendation is an offer_id
What I don't understand is why you're trying to POST with a link? Does creating a recommendation only require an offer id?
In your link_to you're only specifying a controller, you need to also specify the action otherwise it doesn't know where to route it to. Either use:
<%= link_to({ controller: 'recommendations', action: 'show', id: offer.id }) do %>
<p>Some Html</p><%= offer.recommendations %>
<% end %>
Or
<%= link_to({ show_recommendations_path(id: offer.id) }) do %>
<p>Some Html</p><%= offer.recommendations %>
<% end %>