# Is it possible...?

**URL:** <https://www.ruby-forum.com/t/is-it-possible/190915>\
**Category:** Rails\
**Created:** [June 30, 2010, 5:17pm UTC](https://www.ruby-forum.com/t/is-it-possible/190915 "2010-06-30T17:17:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon\_comp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/898d66/32.png) [@anon\_comp](https://www.ruby-forum.com/u/anon_comp)\
**Post date:** [June 30, 2010, 5:17pm UTC](https://www.ruby-forum.com/t/is-it-possible/190915/1 "2010-06-30T17:17:20Z")

</div>

I’ve made a scaffold project that has been edited so that when you  
create a new task, the show method will run though some ruby code that  
manipulates the data inputed. I want to make it so that when a user  
types in the url  
[http://localhost:3000/some\_controller/some\_method/some\_id\_#](http://localhost:3000/some_controller/some_method/some_id_#),  
the ruby code won’t run again. I tried to fix this problem by telling  
the program to check if the id exsist then don’t run the ruby code and  
redirect the user, otherwise run the code. This didn’t work becuase  
the id does get created.

The other solution I tried was to create an after\_filter on the create  
method. The issue with this was that I couldn’t get the parameters  
from the form and manipulate it.

I appreciate all the help I can get!

Thanks,  
Anon\_comp

---

<div class="post-metadata">

**Author:** ![anon\_comp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/898d66/32.png) [@anon\_comp](https://www.ruby-forum.com/u/anon_comp)\
**Post date:** [June 30, 2010, 5:34pm UTC](https://www.ruby-forum.com/t/is-it-possible/190915/2 "2010-06-30T17:34:09Z")

</div>

anon\_comp wrote:

> I’ve made a scaffold project that has been edited so that when you  
> create a new task, the show method will run though some ruby code that  
> manipulates the data inputed. I want to make it so that when a user  
> types in the url  
> [http://localhost:3000/some\_controller/some\_method/some\_id\_#](http://localhost:3000/some_controller/some_method/some_id_#),  
> Can you explain in some more detail what you’re trying to do here?

> the ruby code won’t run again. I tried to fix this problem by telling  
> the program to check if the id exsist then don’t run the ruby code and  
> redirect the user, otherwise run the code. This didn’t work becuase  
> the id does get created.  
> Ok, well then there’s a bug in your code: don’t give up, try and fix it.  
> Post your code up please.
> 
> The other solution I tried was to create an after\_filter on the create  
> method. The issue with this was that I couldn’t get the parameters  
> from the form and manipulate it.  
> The code in an after filter can access params. Post your code up  
> please.

---

<div class="post-metadata">

**Author:** ![anon\_comp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/898d66/32.png) [@anon\_comp](https://www.ruby-forum.com/u/anon_comp)\
**Post date:** [June 30, 2010, 5:58pm UTC](https://www.ruby-forum.com/t/is-it-possible/190915/3 "2010-06-30T17:58:20Z")

</div>

On Jun 30, 11:34 am, Max W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Can you explain in some more detail what you’re trying to do here?

Basically taking a file that the user browsed on their local computer  
(in this case my own computer since I’m testing it), manipulate the  
file directory name and run it through an outside program. The outside  
program creates a file that the user can view and I don’t want the  
ruby code activating again when the user reopens the show page.

> Ok, well then there’s a bug in your code: don’t give up, try and fix it.  
> Post your code up please.

This is the code for my first solution:

## In Controller

def show  
@order = Order.find(params[:id])  
respond\_to do |format|  
if @order then  
format.html {redirect\_to ‘/display’, :notice =\> “This task has  
already been created”}  
format.xml {render :xml =\> @order}

```
  else
    order = @order.logfile
    @log_file_cut = order.gsub(/[\w ! # $ % ^ & * + -]+\.log$/,

```

‘\*.log’)  
system(‘C:\analog\_6.0\analog.exe’, @log\_file\_cut)

```
    time = @order.created_at.strftime("%m%d%y_%H%M")
    watchforfile = "C:\\rails_project\\test2_copy\\public\\output\

```

\errors.txt"  
File.rename watchforfile, watchforfile+“.#{time}.html”

```
    StatusReport.deliver_confirm(@order) #sends email when analog

```

has completed

```
    format.html {redirect_to(:action => 'edit')} # show.html.erb
    format.xml { render :xml => @order }
  end
end

```

rescue ActiveRecord::RecordNotFound  
logger.error(“Attempt to access invalid task #{params[:id]}”)  
flash[:notice] = “Invalid task”  
redirect\_to “/display”  
end

If you think about it, it’ll run the first set of code when I create a  
new object, not the actual ruby code to runs the outside program

> > The other solution I tried was to create an after\_filter on the create  
> > method. The issue with this was that I couldn’t get the parameters  
> > from the form and manipulate it.
> 
> The code in an after filter can access params. Post your code up  
> please.

## My Second Solution In Controller

after\_filter :analog, :only =\> [:create]

protected  
def analog  
order = params[:logfile]  
@log\_file\_cut = order.gsub(/[\w ! # $ % ^ & \* + -]+.log$/,  
‘\*.log’)  
system(‘C:\analog\_6.0\analog.exe’, @log\_file\_cut)  
end  
end

Thanks for the help 🙂

---

<div class="post-metadata">

**Author:** ![anon\_comp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/898d66/32.png) [@anon\_comp](https://www.ruby-forum.com/u/anon_comp)\
**Post date:** [June 30, 2010, 6:07pm UTC](https://www.ruby-forum.com/t/is-it-possible/190915/4 "2010-06-30T18:07:08Z")

</div>

In your first example, is the show action ever called with an id for an  
order that doesn’t exist? Normally show would never be called with an  
invalid id. You say "If you think about it, it’ll run the first set of  
code when I create a  
new object, " but that doesn’t make sense to me. It won’t run either  
set because when you create an object you do it in the create action,  
not the show action.

You need to explain more about what your process is here: i don’t mean  
post tons of code up but explain the sequence of events.

In your second example, what’s the actual problem? I would look in your  
log to see what params[:logfile] is and then step through your code and  
see what it’s doing.
