My Rails button is not updating the database - ruby-on-rails

I created a button where users can input stuff in a field and then press the button to update the database (put request) which can be seen here in show.html.erb:
<% provide(:title, #user.name) %>
<div class="row">
<aside class="col-md-4">
<section class="user_info">
<h1>
<%= gravatar_for #user %>
<%= #user.name %>
<br>
<%= #user.email %>
<% if #errors %>
<p>THE FORM COULD NOT BE SAVED </p>
<ul id='errors'>
<% #errors.each do |error| %>
<li><%= error %></li>
<% end %>
</ul>
<% end %>
<br>
<% if is_admin? %>
<% if !#user.admin %>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_for(#user) do |f| %>
<%= f.label :wistia_project_id %>
<%= f.text_field :wistia_project_id, class: 'form-control' %>
<%= f.submit "Save", :action => "set_wistia_project_ID", :method => :patch, :form_class => "form-control" %>
<% end %>
</div>
</div>
<% end %>
<% end %>
</h1>
</section>
</aside>
</div>
The function is in user_controller.rb:
# Sets wistia_project_ID.
def set_wistia_project_ID
#user = User.find(params[:id])
#user.set_project_id
unless #user.valid?
#errors = #user.errors.full_messages
render :show
end
end
That function calls another function, just to separate things more clearly. This other function lives in user.rb:
# Sets the wistia_project_ID.
def set_project_id!(val)
self.wistia_project_ID = val # self is necessary here
save # or self.save, but the self is unnecessary here
end
My routes.rb:
.
.
.
resources :users do
member do
patch 'set_wistia_project_ID'
end
end
My problem is that right now, when you press the button, it says: Completed 500 Internal Server Error in 26ms (ActiveRecord: 0.7ms)
and
NoMethodError (undefined method `set_project_id' for #<User:0x000055b1a0914ab8>
2019-06-26T14:46:34.940086+00:00 app[web.1]: Did you mean? wistia_project_id):

Zavitoski got it right. I suggest, however, that you're doing a number of things more fundamentally wrong. Given that you're early in your rails journey, I hope you don't mind if I point a few things out.
First, and to be nit-picky, yes, you created a button. But, it is not a button "where users can input stuff in a field and then press the button to update the database". You created a button on a form. And you created a field on that form. The user can input stuff into the field. And when clicked, the button submits the form which includes the information in the field.
Now, on that form, you did:
<%= form_for(#user) do |f| %>
<%= f.label :wistia_project_id %>
<%= f.text_field :wistia_project_id, class: 'form-control' %>
<%= f.submit "Save", :action => "set_wistia_project_ID", :method => :patch, :form_class => "form-control" %>
<% end %>
There are a few things wrong with:
:action => "set_wistia_project_ID"
First, set_wisteria_project_ID is not a very ruby-ish action name. set_wistia_project_id would be more like it. Also, you're using old-form key-value formatting. And, you can use a symbol instead of a string for your action name so your code is prettier. Something, perhaps, like:
<%= f.submit "Save", action: :set_wistia_project_id, method: :patch, form_class: "form-control" %>
But, that's a mistake, too. Because you don't need a set_wistia_project_id action. (It's an action or a method, not a function.) You already have the update action. And form_for is smart enough to submit to this action if #user is an instance of User. So, really, you should do:
<%= form_for #user do |f| %>
<%= f.label :wistia_project_id %>
<%= f.text_field :wistia_project_id, class: 'form-control' %>
<%= f.submit "Save", form_class: "form-control" %>
<% end %>
I'm not sure what form_class is, but I'll trust that it's correct.
Now, in your UsersController, just do:
class UsersController < ApplicationController
def update
#user = User.find(params[:id])
if user.update(user_params)
# do something successful
else
# do something unsuccessful
end
end
private
def user_params
# NOTE: You'll probably want to permit other stuff here, too.
params.require(:user).permit(:wistia_project_id)
end
end
Get rid of this:
class User < ApplicationRecord
# Sets the wistia_project_ID.
def set_project_id!(val)
self.wistia_project_ID = val # self is necessary here
save # or self.save, but the self is unnecessary here
end
end
Because you're just duplicating the update method. And, you probably want that attribute to be wistia_project_id, not wistia_project_ID. (Again, you never see _ID as the suffix in rails core and you might as well be conventional.) And, if you make sure you have your association set up correctly, ActiveRecord should make sure that wistia_project_id is actually a valid value.
And write your routes.rb like this:
resources :users
Because you don't need all that set_wistia_project_id business.

It appears that you are not calling the function by the name you defined, neither passing the parameter (project_id) needed.
def set_wistia_project_ID
#user = User.find(params[:id])
#user.set_project_id!(params[:wistia_project_id])
unless #user.valid?
#errors = #user.errors.full_messages
render :show
end
end
This should use the function you created and pass the parameter from the form.

Related

Render in a different order on the page breaks the routing

I have Challenges containing Puns, and it is possible to vote on puns. On the Challenge Show page, all puns are rendered and show their votes count. This is currently on the view page:
<%= render #challenge.puns.reverse %>
<br>
<div id="form">
<%= render "puns/form" %>
</div>
I want the puns form to appear above the items (puns) already submitted. But if swap them around, like this:
<div id="form">
<%= render "puns/form" %>
</div>
<%= render #challenge.puns.reverse %>
I get a controller error and pun.id is not suddenly not available and the voting link breaks.
No route matches {:action=>"upvote", :challenge_id=>"9", :controller=>"puns", :id=>nil}, missing required keys: [:id]
Here is the puns/form part that is causing the issue
<% if signed_in? %>
<% if current_user.voted_for? pun %>
<%= pun.votes_for.size %>
<span class="pun_text"><%= link_to pun.pun_text, challenge_pun_path(#challenge, pun.id) %></span>
<% else %>
<%= link_to like_challenge_pun_path(#challenge, pun.id), method: :put do %>
<span class="heart_like">❤</span> <%= pun.votes_for.size %>
<% end %>
<span class="pun_text"><%= link_to pun.pun_text, challenge_pun_path(#challenge, pun.id) %></span>
<% end %>
<% end %>
It is the like_challenge_pun_path that throws an error but I cannot understand why. I am declaring #challenge again here, so it should be able to get the id.
Here is the form for the puns:
<%= form_for([#challenge, #challenge.puns.build]) do |f| %>
<span class=".emoji-picker-container">
<%= f.text_area :pun_text, placeholder: "Add pun", data: { emojiable: true } %>
</span>
<%= f.submit %>
<% end %>
Also, here is my routes setup
resources :challenges do
resources :puns do
member do
put "like", to: "puns#upvote"
put "dislike", to: "puns#downvote"
end
end
end
and the corresponding action to upvote
def upvote
#pun = #challenge.puns.find(params[:id])
#pun.upvote_by current_user
redirect_to #challenge
end
Can anyone help?
I think the code is for the puns collection.
I assume the issue is that in the form you have something like:
#challenge.puns.build
So in #challenge.puns collection appears not persisted record (without id), so path for this model cannot be generated.
As a quick solution I suggest:
<%= render #challenge.puns.reverse.select(&:persisted?) %>
UPDATE:
As I assumed you have
<%= form_for([#challenge, #challenge.puns.build]) do |f| %>
You can also try:
<%= form_for([#challenge, Pun.new]) do |f| %>
Or solve it in the controller. But need to see controller code for it.

NoMethodError in FrontPages#home when I try to add a form

I am pretty sure it's a very noob mistake, but I don't understand why I can't add my form directly to my homepage without causing a NoMethodError.
This is my current setup, and it works perfectly fine.
view/front_pages/home.html.erb (my home page)
<!--declare :title to be SHOULD I GET THIS-->
<% provide(:title, "SHOULD I GET THIS") %>
<div class = "container">
<h1>Should I really spend money on this?</h1>
<h2>Let's crunch in some numbers and find out...</h2>
<%= link_to "Get Started", calculate_path, class: "btn btn-info " %>
</div>
view/users/new.html.erb (where my form is right now)
<% provide(:title, "Calculate")%>
<div class = "form-group container">
<%= simple_form_for #user do |form|%>
<%= form.error_notification%>
<%= form.input :price%>
<%= form.input :wallet%>
<%= form.button :submit, "Submit", class: "submit"%>
<%end%>
</div>
controllers/users_controller.rb
class UsersController < ApplicationController
def new
#user = User.new
end
end
However, I want the form to be directly on my home page so the users don't need to click an extra button to get to the form. I tried
view/front_pages/home.html.erb
<!--declare :title to be SHOULD I GET THIS-->
<% provide(:title, "SHOULD I GET THIS") %>
<div class = "container">
<h1>Should I really spend money on this?</h1>
<h2>Let's crunch in some numbers and find out...</h2>
<%= link_to "Get Started", calculate_path, class: "btn btn-info " %>
</div>
<div class = "form-group container">
<%= simple_form_for #user do |form|%>
<%= form.error_notification%>
<%= form.input :price%>
<%= form.input :wallet%>
<%= form.button :submit, "Submit", class: "submit"%>
<%end%>
</div>
and it returns me the NoMethodError. I thought it's because I did not initiate a #user variable in the front_page controller, so I tried
class FrontPagesController < ApplicationController
def new
#user = User.new
end
def home
end
end
but it still does not work. I am thinking it's some concept about MVC that I still am not quite grasping. What is wrong with my code and what should I keep in mind next time so I don't make the same mistake?
PS: I use simple_form gem to generate my form
Edit: The error message is undefined method `model_name' for NilClass:Class
Error is quite simple:
Undefined method `model_name' for NilClass:Class
This error means you're trying to call the model_name method on a variable which is not populated with any data. You are not calling this method; form_for is - meaning you basically need to have #user declared in your controller, as you rightly pointed out:
class FrontPagesController < ApplicationController
def new
#user = User.new
end
def home
end
end
--
#user
The problem is that you're using the home action - you're declaring your #user variable in the new action. This means it won't be set, as it won't be called.
You'll be best doing this:
class FrontPagesController < ApplicationController
def home
#user = User.new
end
end
This will make #user available in your home action, which should resolve the error for you!
You need specify the action for the form
<%= form_for :user, url: { action: create } do |f| %>
or you can use your path helper if you have a resource defined for user
<%= form_for :user, url: user_path do |f| %>

Repopulate form after error validation from another controller Rails 4.0

I am really embarrassed with this problem :
I want to post comments from one view and if errors occurs in form, i want the form to be repopulated. I use the render method but my form isn't repopulated.
I specify that the form is displayed from a view and use another controller action, by other words means :
Form called from : views/cars/show.html.erb code below :
<h1>Fiche détaillée</h1>
<%= #car.marque %><br>
<%= #car.modele %><br>
<%= #car.nbkm %><br>
<%= #car.couleur %><br>
<%= #car.disponibilite %><br>
<hr>
<% x=0 %>
<h1><%= pluralize(#car.comments.count, 'commentaire') %></h1>
<% #car.comments.each do |k| %>
<%= x+=1 %>
Email : <%= k.email %><br>
Sujet : <%= k.sujet %><br>
Commentaire : <%= k.commentaire %><br>
<%= link_to 'Supprimer', [k.car, k], method: :delete %><br><br>
<% end %>
<hr>
<h1>Ajouter votre commentaire</h1>
<div style='width:300px;'>
<% flash.each do |key, msg| %>
<% if msg.count >0 %>
<p class="bg-danger" style='padding:10px;'><%= pluralize(msg.count,'error') %>
<ul><% msg.full_messages.each do |m|%>
<li><%= m %></li>
<% end %>
</p>
<% end %>
<% end %>
</ul>
<%= form_for([#car,#car.comments.build]) do |co| %>
<%= co.label :'Email' %><br>
<%= co.text_field :email , class: 'form-control' %><br>
<br>
<%= co.label :'Sujet' %><br>
<%= co.text_field :sujet , class: 'form-control'%><br>
<br>
<%= co.label :'Commentaire' %><br>
<%= co.text_area :commentaire , class: 'form-control' %><br>
<br>
<%= co.submit :'Envoyer votre commentaire', class: 'btn btn-info'%>
<% end %>
</div>
below my controllers :
Controller 1 : controllers/cars_controller.rb
def create
#render text: params[:car].inspect
#car = Car.new(params[:car].permit(:marque,:modele,:nbkm,:couleur,:disponibilite))
if !#car.save
render 'new'
else
redirect_to #car
end
end
def show
#car = Car.find(params[:id])
end
def index
#cars=Car.all
end
Controller 2 : controllers/comments_controller.rb
class CommentsController < ApplicationController
def new
#comment=Comment.new
end
def create
#car = Car.find(params[:car_id])
#comment = #car.comments.create(params[:comment].permit(:email,:sujet,:commentaire))
if !#comment.save
flash[:error] = #comment.errors
flash.keep[:error]
render 'cars/show'
else
redirect_to car_path(#car)
end
end
def destroy
#car = Car.find(params[:car_id])
#comment = #car.comments.find(params[:id])
#comment.destroy
redirect_to car_path(#car)
end
end
I really don't understand why it does not work !!
Thank you so much for any assistance ;)
Edited:
I did some similar tests a bit for your case, the problem should due to the flash method.
Replace below lines:
flash[:error] = #comment.errors
flash.keep[:error]
render 'cars/show'
To:
flash.now[:error] = #comment.errors.full_messages
#car.reload
render 'cars/show'
Because flash[:error] will only be available in next action, means only works in redirect_to, so you have to use flash.now[:error] for rendering same view template. And most importantly, though the save failed of the #comment, the comment list in #car will still receive an instantiated invalid object return by the comment's create, build, or new method. It is because these three methods will always return instantiated object to the #car.comments collection, though it is failed to save it. So we must reload the #car object by #car.reload to refresh the memory and get correct Comment collection from the database.
Previous response:
In crate action, the create method #car.comments.create(..) will directly create and return an instantiated object without given attributes then try to save it if it passed the validation. If you have not set validations for Comment model, then it will directly save it. Try #car.comments.new(..) or #car.comments.build(..) for collection associations, it will not force to save an instantiated object after the validation passed. Also, check your Comment model for setting the validations.

Adding a user_id to a Message model

I currently have a simple app that includes user authentication through devise and a message model(the message model uses Jquery and Faye). Both are working fine independently, but I would like to add a user_id to the messages.
I have already updated the schema and models with the relationship, but I am having trouble inputting the necessary code to have the view and controller input the relationship into the db, while keeping the jquery working. I wasn't sure of the best way, but here I tried to create a hidden field that would pull the user_id, not sure if this is even possible. Here is the applicable code, any help is appreciated.
Message index.html.erb
<ul id="chat">
<%= render #messages %>
</ul>
<%= form_for Message.new, :remote => true do |f| %>
<div class="field">
<%= f.text_field :content %>
</div>
<div class="field">
<%= f.hidden_field :user_id %>
</div>
<div class="actions">
<%= f.submit "Send" %>
</div>
<% end %>
create.js.erb for messages
<% broadcast "/messages" do %>
$("#chat").append("<%= escape_javascript render(#user.message) %>");
<% end %>
$("#new_message")[0].reset();
Messages Controller
class MessagesController < ApplicationController
def index
if #authentications = current_user
#messages = Message.all
else
redirect_to authentications_url
flash[:notice] = "You need to sign in before answering questions"
end
end
def create
#user = User.find(params[:user_id])
#message = #user.message.create(params[:message])
end
end
I think you have everything you would need, but if not, let me know and I will be happy to provide it for you.
Thanks, everyone.
two things to correct,
1)use user association to create message instance in form(probably current_user if logged-in user create a message)
<%= form_for user.messages.new, :remote => true do |f| %> #// assuming its has many association
2) if it is has_many association then change association in create action
#message = #user.messages.create(params[:message])

RoR appended ?format= on form submit

I'm creating a little newsletter application, with 'double opt-in restrictions', when I simply fill in my form (subscription page) and submit the form I get redirected to my subscribed page (which is all normal) however my form appends a querystring to my action attribute of my form (http://localhost:3000/newsletter/subscribe?format=)
routes:
match 'newsletter/subscription' => 'newsletter_subscriptions#subscription'
post 'newsletter/subscribe' => 'newsletter_subscriptions#subscribe'
controller:
class NewsletterSubscriptionsController < ApplicationController
respond_to :html
# GET /newsletter/subscription
def subscription
respond_with (#subscription = NewsletterSubscription.new)
end
# POST /newsletter/subscribe
def subscribe
# If there's already an unconfirmed record with the submitted email, use that object otherwise create a new one based on the submitted email
sub_new = NewsletterSubscription.new
sub_new.email = params[:newsletter_subscription]['email']
sub_old = NewsletterSubscription.find_by_email_and_confirmed sub_new.email, 0
#subscription = sub_old || sub_new
if #subscription.save
Newsletter.delay.subscribed(#subscription) # with delayed_job
else
render :action => "subscription"
end
end
...
end
view (newsletter_subscription/subscription.html.erb):
<h1>New newsletter_subscription</h1>
<%= form_for(#subscription, :url => newsletter_subscribe_path(#subscription)) do |f| %>
<% if #subscription.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#subscription.errors.count, "error") %> prohibited this newsletter_subscription from being
saved:</h2>
<ul>
<% #subscription.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :email %>
<br/>
<%= f.text_field :email %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
PS: I would be pleased if someone could evaluate my ruby code please (posted above), I'm still learning a lot and would like to see some 'guidelines' or feedback, I think I still can learn a lot.
Try removing the #subscription argument you're passing into newsletter_subscribe_path. Since there isn't an :id in the route and it's a new object, passing it doesn't really make sense. I'm assuming that's what is being interpreted as the format.
<%= form_for(#subscription, :url => newsletter_subscribe_path) do |f| %>
As for improvements you can make to the code, the biggest thing I see is moving the old/new subscription logic into the model.
# in NewsletterSubscription
def self.with_email(email)
find_by_email_and_confirmed(email, 0) || new(:email => email)
end
# in controller
#subscription = NewsletterSubscription.with_email(params[:newsletter_subscription]['email'])
if #subscription.save
#...
Also respond_to and respond_with aren't really necessary here since you're just dealing with HTML views. You can remove that.

Resources