0

I'm trying to learn web development as I go (I just need need to get this one project done. I don't plan on touching the subject ever again.) and I've run into the problem of getting data from a rails web page to its corresponding controller. My end goal is to get data from javascript variables and pass that to ruby, but I've decided to take small steps so for now I'm trying to get a button_to to send some hard coded strings from the new.html.erb to the corresponding create method in the controller. I've probably tried a hundred combinations of view, controller and, routs code and I can't get any of them to work. Here is the current iteration of my code for the controller, view, and routes (I'm not sure if routes even matters).

ponies_controller.rb

def create(name, pro)                                                                                                                                                                                                                                                                                                  
    #@pony = Pony.new(params[:id])                                                                                                                                                                                                                                                                                       
    @pony = Pony.new(name: name, profession: pro)                                                                                                                                                                                                                                                                                                                                                                                                     
    respond_to do |format|                                                                                                                                                                                                                                                                                               
      if @pony.save                                                                                                                                                                                                                                                                                                      
        format.html { redirect_to @pony, notice: 'Pony was successfully created.' }                                            |~                                                                                                                                                                                          
        format.json { render :show, status: :created, location: @pony }                                                                                                                                                                                                                                                  
      else                                                                                                                                                                                                                                                                                                               
        format.html { render :new }                                                                                                                                                                                                                                                                                      
        format.json { render json: @pony.errors, status: :unprocessable_entity }                                                                                                                                                                                                                                         
      end                                                                                                                                                                                                                                                                                                               
    end                                                                                                                                                                                                                                                                                                              

end

new.html.erb

<h1>New Pony</h1> 
<%= render 'form', pony: @pony %>
<%= link_to 'Back', ponies_path %>
<%= button_to "create_pony", {action: create("s","ss")}, remote: true,from_class: "create_pony" %>

routes.rb (Not sure if this is important)

Rails.application.routes.draw do
    resources :ponies
    # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
    post 'ponies/:id/create' => 'pony#create', as: :create_pony
end

While this code doesn't work I think it shows how I think things should work. I feel like I should just be able to call the method and be done with things, but that is obviously wrong. What should I be doing to get button_to to post a new "pony"?

8
  • 1
    @Lighting JImmy Jor Johnson do ever read any rails book or basic tutorails ? Commented Apr 11, 2017 at 4:55
  • You are missing the form input fields and therefore do not actually post any data to the application. Commented Apr 11, 2017 at 5:04
  • @Vishal Not past the first 3 chapters of railstutorial, but like I mentioned before this is just a small one time project that I want to get done. I don't really care about knowing everything about rails I just want to finish my project. Commented Apr 11, 2017 at 5:04
  • @spickermann form input fields? Commented Apr 11, 2017 at 5:07
  • 1
    That resource route should already cover what you're defining with the post method, so that second thing should be removed. Also, use link helpers to route things properly: new_pony_path is the intention here. Note that controller methods do not take arguments. Everything will come through params. Commented Apr 11, 2017 at 5:58

1 Answer 1

1

Here's the Rails way to do this in the simplest way (assuming you've set up your Pony model correctly):

# ponies_controller.rb
def create   
  @pony = Pony.new(pony_params)
  if @pony.save
    # success
  else 
    # errors
  end
end

private

def pony_params
  params.require(:pony).permit(:name, :profession) # whitelist the parameters you want to accept from the pony creation form 
end  

and your pony form should like this

# new.html.erb
<h1> New Pony </h1>
<%= form_for @pony, remote: true do |f| %> 
  <%= f.text_field :name %> # this will be passed to the controller in the params hash 
  <%= f.text_field :profession %> # this too
  <%= f.submit %>
<% end %>

also, resources :ponies will create all the routes you need for ponies so no need to define one yourself.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.